[Bug c++/12680] New: C++ destructor called too early on temporary objects
drtr at dial dot pipex dot com
gcc-bugzilla@gcc.gnu.org
Sun Oct 19 16:52:00 GMT 2003
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12680
Summary: C++ destructor called too early on temporary objects
Product: gcc
Version: 3.3.2
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: drtr at dial dot pipex dot com
CC: gcc-bugs at gcc dot gnu dot org
When a temporary object is created, the C++ standard requires the destructor to
be called at the end of the enclosing block, not when the object goes out
of scope. In the example below, g++ prints
node(5)
~node(5)
node(3)
~node(3)
node(4)
~node(4)
whereas correct output would be
node(5)
node(3)
node(4)
~node(4)
~node(3)
~node(5)
#include <stdio.h>
using namespace std;
class node
{
int x;
public:
node(int v) : x(v) { printf("node(%d)\n", v); }
node(const node &n) : x(n.x) { printf("node(node::%d)\n", n.x); }
node &operator=(const node &n)
{
x = n.x;
printf("node = %d\n", n.x);
return *this;
}
node &operator=(int v)
{
x = v;
printf("node = int %d\n", v);
return *this;
}
~node() { printf("~node(%d)\n", x); }
void print() const { printf("node: %d\n", x); }
bool operator<(const node &n) const { return x < n.x; }
};
void
test(const node &n)
{
}
int
main()
{
{
test(node(5));
test(node(3));
test(node(4));
}
}
More information about the Gcc-bugs
mailing list