>>> No, because you can always bind it to a reference.
>> I could assume that no one would be so stupid to do sth. like this:
>> Obj const &o = Obj();
>> So if I make this assumption, what's the solution ?
> The lifetime of the temporary will be extended to the lifetime of the
> reference in that case, so it's not completely stupid.
You're right, and look at this:
#include <iostream>
using namespace std;
struct S
{
S()
{
cout << "S::S()" << endl;
}
~S()
{
cout << "~S::S()" << endl;
}
};
int main( int argc, char **argv )
{
S const &s = argc < 0 ? S() : S();
cout << "after destruction?" << endl;
}
Of course argc can never be < 0, but the compiler doesn't know that.
So look at the output:
S::S()
~S::S()
after destruction!
~S::S()
So the code destructs only one object within the statement !