Re: reference lifetimes...
On Nov 10, 9:20 pm, "James" <n...@spam.invalid> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
On Nov 10, 7:22 pm, "James" <n...@spam.invalid> wrote:
Here is my code:
Why does the const reference not properly maintain its
lifetime over the call to 'cout' when the reference is
contained within a POD 'foo_holder'?
I get the following output:
Something seems terribly wrong here... Is there anyway to
Use a different compiler? I get
using VC++. (G++ behaves as you describe, however. This
looks like a bug in g++.)
What version of VC++ are you using?
I just tried the code using the following version:
Microsoft Visual Studio 2008
Version 9.0.30729.1 SP
Microsoft .NET Framework
Version 3.5 SP1
Installed Edition: VC Express
and I get the following output:
What the heck is going on!?
It sounds like they've changed it.
Please forgive my ignorance of the C++ standard, but:
Humm... Are you sure that lifetime of an is extended when
assigned to a constant reference residing in a POD using
explicit initializer braces?
POD has little to do with it. You're using aggregate
initialization. foo_holder is an aggregate, so each element is
initialized by the corresponding initialization expression,
using copy initialization. In other words, your two cases must
have exactly the same behavior, according to the standard.
(I'm curious, however. Since the behavior has changed in VC++9,
they've obviously changed it. Why? Did they accidentally
introduce a new bug, which curiously mimics exactly the behavior
of g++, or is there something I'm overlooking?)
Can a POD legally include a constant reference to an object?
No, but it doesn't matter. The key here is that you're using
aggregate initialization, so you're not initializing the
foo_holder, you're initializating each of its members.