J
Jonathan Turkanis
Dear All,
I'm using VC7.1.
The test program at the end of this message initializes a member
variable of reference type with a temporary. I believe the program
should output
counter = 0;
counter = 0;
indicating that the total number of objects of the temporary's type is
0 at the beginning and the end of main().
Instead, I get the output
counter = 0;
counter = 1;
indicating that the destructor of (at least one copy of) the temporary
is never called.
This program produces the expected output when compiled using Intel
8.0 for Windows, Comeau 4.3.3, GCC 3.2 (MinGW), Codewarrior 8.3 and
Borland 5.6.4.
Is this a bug, or is my program illegal for some reason? If it is a
bug, are there any known workarounds?
Best Regard,
Jonathan Turkanis
//--------Test program -------------------------//
#include <iostream>
int counter;
struct ref {
ref () { ++counter; }
ref (const ref &) { ++counter; }
~ref () { --counter; }
};
struct test {
test() : r(ref()) { }
const ref & r;
};
int main()
{
std::cout << "counter = " << counter << "\n";
{
test t;
}
std::cout << "counter = " << counter << "\n";
}
I'm using VC7.1.
The test program at the end of this message initializes a member
variable of reference type with a temporary. I believe the program
should output
counter = 0;
counter = 0;
indicating that the total number of objects of the temporary's type is
0 at the beginning and the end of main().
Instead, I get the output
counter = 0;
counter = 1;
indicating that the destructor of (at least one copy of) the temporary
is never called.
This program produces the expected output when compiled using Intel
8.0 for Windows, Comeau 4.3.3, GCC 3.2 (MinGW), Codewarrior 8.3 and
Borland 5.6.4.
Is this a bug, or is my program illegal for some reason? If it is a
bug, are there any known workarounds?
Best Regard,
Jonathan Turkanis
//--------Test program -------------------------//
#include <iostream>
int counter;
struct ref {
ref () { ++counter; }
ref (const ref &) { ++counter; }
~ref () { --counter; }
};
struct test {
test() : r(ref()) { }
const ref & r;
};
int main()
{
std::cout << "counter = " << counter << "\n";
{
test t;
}
std::cout << "counter = " << counter << "\n";
}