Newsgroup: comp.lang.c++
Subject: Lifetime of temporaries bound to references in C++11
From: Michael Mehlich <michael@...>
Date: Fri, 05 Jul 2013 18:01:46 -0500
I noticed an unexpected order of destructor calls for temporaries
bound to references wrt the lifetime of the enclosing object
when experimenting with C++11 using gcc 4.9.0.
In particular, the program included below produced the output
done
~C
~B
~D
which is counter to my expectation of
done
~D
~C
~B
Looking at the C++11 standard, 12.2p5 clearly requires
~C to be called before ~B (reverse order of completion of
construction). However, the ordering of object vs. temporary
is specified wrt. the moment the object/temporary is created.
Searching the standard, it isn't clear to me when an object
or temporary is actually considered as "created", in particular
wrt. the evaluation of the constructor.
What (and where) does the standard actually say about the relative
lifetimes/ the order of destruction in this scenario?
--
Michael
extern "C" int printf(const char *, ...);
struct B {
~B() { printf("%s\n",__func__); }
int x;
int y;
};
struct C {
~C() { printf("%s\n",__func__); }
int x;
const B &b;
int y;
};
struct D {
~D() { printf("%s\n",__func__); }
int x;
const C &c;
int y;
};
int main() {
D d = { 1, { 2, { 3, 4 }, 5 } , 6 };
printf("done\n");
}
--- news://freenews.netfront.net/ - complaints: news@... ---
Subject: Lifetime of temporaries bound to references in C++11
From: Michael Mehlich <michael@...>
Date: Fri, 05 Jul 2013 18:01:46 -0500
I noticed an unexpected order of destructor calls for temporaries
bound to references wrt the lifetime of the enclosing object
when experimenting with C++11 using gcc 4.9.0.
In particular, the program included below produced the output
done
~C
~B
~D
which is counter to my expectation of
done
~D
~C
~B
Looking at the C++11 standard, 12.2p5 clearly requires
~C to be called before ~B (reverse order of completion of
construction). However, the ordering of object vs. temporary
is specified wrt. the moment the object/temporary is created.
Searching the standard, it isn't clear to me when an object
or temporary is actually considered as "created", in particular
wrt. the evaluation of the constructor.
What (and where) does the standard actually say about the relative
lifetimes/ the order of destruction in this scenario?
--
Michael
extern "C" int printf(const char *, ...);
struct B {
~B() { printf("%s\n",__func__); }
int x;
int y;
};
struct C {
~C() { printf("%s\n",__func__); }
int x;
const B &b;
int y;
};
struct D {
~D() { printf("%s\n",__func__); }
int x;
const C &c;
int y;
};
int main() {
D d = { 1, { 2, { 3, 4 }, 5 } , 6 };
printf("done\n");
}
--- news://freenews.netfront.net/ - complaints: news@... ---
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?36558-Lifetime-of-temporaries-bound-to-references-in-C-11&goto=newpost
View all the progranning help forums at:
http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums
No comments:
Post a Comment