example code: #include <iostream> using namespace std; struct test { test () { cout << "test::test()" << endl; } ~test() { cout << "test::~test()" << endl; } };
struct a { const test &x; a () : x(test()){ cout << "a::a()" << endl; } ~a() { cout << "a::~a()" << endl; } }; int main() { a obj; } Expected result: test::test() a::a() test::~test() a::~a() Actual result: test::test() test::~test() a::a() a::~a() Std sect 12.2 Temporary objects 3 (...) Temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created. (...) 4 There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. (...) 5 The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference expect: - A temporary bound to a reference member in a constructor's ctor-initializer persists until the constructor exits. -- Summary: object destroyed at wrong time Product: gcc Version: 4.4.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: miklcct at gmail dot com GCC target triplet: x86_64-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42043