问题
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
回答1:
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.
回答2:
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.
回答3:
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;
}
来源:https://stackoverflow.com/questions/15267676/reference-to-an-unnamed-temporary-object-life-time