This outputs F~
but I was expecting ~F
#include <iostream>
struct Foo {
int _x;
operator const int & () const {return _x;}
~ Foo () {std :: cout << "~";}
};
void foo (const int &)
{
std :: cout << "F";
}
int main ()
{
foo (Foo ());
}
I constructed this as a counterexample to show that the most-important-const is an exception rather than a rule. It is normally written as
when a const reference binds to a temporary, then the lifetime of that temporary is extended to the lifetime of the reference
I was trying to illustrate that, although Foo()
is a temporary, the reference to _x
returned by the conversion operator is not, and that the above code is unsafe.
But the output seems to prove that the example is safe, the lifetime of the temporary Foo()
is extended by the existence of a const reference to one of its members.
Is this right? Where in the standard is this specified?
The general rule, regarding temporaries, is that their life ends when the full expression they are part of ends (informally, when reaching the ;
).
12.2 Temporary objects
3/ [...] Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception. The value computations and side effects of destroying a temporary object are associated only with the full-expression, not with any specific subexpression.
That's because the temporary survives for the whole duration of function call. When you do foo (Foo ());
here's what happens:
Foo
is contructed, thenoperator const int&
is called on the temporaryfoo()
is called and this outputs F
foo()
returns temporary Foo
is destroyed and this outputs ~
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With