Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NOT the most important const.. but what is this?

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?

like image 502
spraff Avatar asked Nov 15 '11 09:11

spraff


2 Answers

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.

like image 139
Matthieu M. Avatar answered Oct 12 '22 15:10

Matthieu M.


That's because the temporary survives for the whole duration of function call. When you do foo (Foo ()); here's what happens:

  1. temporary Foo is contructed, then
  2. operator const int& is called on the temporary
  3. foo() is called and this outputs F
  4. once foo() returns temporary Foo is destroyed and this outputs ~
like image 39
sharptooth Avatar answered Oct 12 '22 13:10

sharptooth