5

After reading this answer from ildjarn, I wrote the following example, and it looks like an unnamed temporary object has the same life time as its reference!

  • How come this is possible?
  • Is it specified in the C++ standard?
  • Which version?

Source code:

#include <iostream>  //cout
#include <sstream>   //ostringstream 

int main ()
{
        std::ostringstream oss;
        oss << 1234;

        std::string const& str = oss.str();
        char        const* ptr = str.c_str();

        // Change the stream content
        oss << "_more_stuff_";
        oss.str(""); //reset
        oss << "Beginning";
        std::cout << oss.str() <<'\n';

        // Fill the call stack
        // ... create many local variables, call functions...

        // Change again the stream content
        oss << "Again";
        oss.str(""); //reset
        oss << "Next should be '1234': ";
        std::cout << oss.str() <<'\n';

        // Check if the ptr is still unchanged
        std::cout << ptr << std::endl;
}

Execution:

> g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> g++ main.cpp -O3
> ./a.out
Beginning
Next should be '1234':
1234
Community
  • 1
  • 1
oHo
  • 51,447
  • 27
  • 165
  • 200
  • 4
    The fact that `const` references extend the lifetime of temporaries should be covered fairly on in your C++ book. – Lightness Races in Orbit Mar 07 '13 at 09:41
  • 1
    You should find your answer if you just try searching the web for the phrase you used: _temporary object has the same life time as its reference_ – Jonathan Wakely Mar 07 '13 at 09:48
  • 1
    possible duplicate of [Lifetime of temporaries](http://stackoverflow.com/questions/4214153/lifetime-of-temporaries) / look what I found – Lightness Races in Orbit Mar 07 '13 at 09:54
  • Hi @LightnessRacesinOrbit. Thank you for your answer but I do not think my question is a duplicate of [Lifetime of temporaries](http://stackoverflow.com/questions/4214153/lifetime-of-temporaries). My question is about a reference. The other question is about function return by value. Please consider both questions are different. Cheers – oHo Mar 07 '13 at 15:08
  • 2
    @olibre: Yeah, you're right. I must have been drunk. – Lightness Races in Orbit Mar 07 '13 at 15:27
  • @LightnessRacesinOrbit: Please do you know how to remove the header **"This question may already have an answer here"**? (because there is no answer to my question there). Cheers ;-) – oHo Mar 07 '13 at 17:26
  • 1
    @olibre: [You cannot](http://meta.stackexchange.com/questions/166427/automatic-insertion-of-dupe-banner-after-just-one-close-vote-is-vulnerable-to-ab). – Lightness Races in Orbit Mar 10 '13 at 22:59
  • @LightnessRacesinOrbit: Thanks for the link, not yet read everything. Just to inform that two people have down-voted this question, and I do not know how many have cancelled their intention to up-vote. I have an idea: I may edit the top of my question to display a message saying this is not a duplicate message... What do you think about this issue? Cheers ;-) – oHo Mar 10 '13 at 23:09
  • 1
    @olibre Please don't. Question content is not for meta things. We have these comments and that is enough until the question is closed and re-opened, one day. This is a flaw in SO's mechanism. Anyway, [the banner _only_](http://meta.stackexchange.com/a/167987/155739) [_appears for you_](http://meta.stackexchange.com/q/168773/155739). – Lightness Races in Orbit Mar 11 '13 at 16:27
  • Thank you @LightnessRacesinOrbit. You are very pedagogical ;-) It is clear for me. Cheers. – oHo Mar 12 '13 at 20:45
  • 1
    @olibre Oh I'm something! – Lightness Races in Orbit Mar 12 '13 at 22:38

3 Answers3

10

How come this is possible?

Because the standard says so, because it's deemed useful. rvalue references and const lvalue references extend the lifetime of temporaries:

[C++11: 12.2/5]: [..] The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference, except [..]

and exhaustive wording in [C++11: 8.5.3/5] requires that we shall not bind temporaries to non-const lvalue references.


Is it specified in the C++ standard? Which version?

Yes. All of them.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
6

A temporary bound to a const reference increases the lifetime of the temporary till the lifetime of the constant reference.

Good Read:

GotW #88: A Candidate For the “Most Important const”


Yes it is specified in the C++ standard from the time references were introduced.
So if you are wondering if this is C++11 feature, no it is not. It already existed in C++03.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
4

Lightness Races in Orbit is right. And I think this example would be more concise.

#include <iostream>  //cout
#include <string>

int main ()
{
    using namespace std;
    int a = 123;
    int b = 123;
//  int       & a_b = a + b; // error!
    int const & a_b = a + b;
    cout<<"hello world!"<<endl;
    cout<<a_b<<endl;
}
Community
  • 1
  • 1
Pony279
  • 403
  • 1
  • 6
  • 12