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 built it as a counterexample to show that the most important const is an exception, not a rule. It is usually written as
when a const reference refers to a temporary one, then the lifetime of this temporary object extends to the lifetime of the link
I tried to illustrate this, although Foo() is temporary, the _x reference returned by the conversion operator is not, and that the above code is unsafe.
But the result, it seems, proves that the example is safe, the lifetime of temporary Foo() extends due to the existence of a const reference to one of its members.
Is it correct? Where does the standard indicate this?
spraff
source share