* Dizzy:
With the exception that a temporary to which a reference (to const) is bound
will exist as long as the reference exists (of course the reference itself
has to be in the same exact scope as the code creating the temporary).
Thus:
MyClass const& ref = getMyClassInstance();
// where getMyClassInstance() returns by value, ie a temporary
The compiler will make sure that the temporary exists as long as ref exists.
I think that was summarized in the immediately following part of my
posting that you elided,
<q>
That is, if I recall correctly -- you shouldn't try to take advantage
of any particular destruction point, so it's mostly irrelevant when that
occurs. If you need the object to persist until the end block, then
declare it as a variable or bind it to a reference to const.
</q>
Probably you didn't see that.
For just about any C++ subject one would care to pick, any explanation
in a Usenet posting will always fall short of being correct when
compared to the standard's full treatment, except if it quotes in full
all that's relevant from the standard (which may be all of it). In this
case the detail you noted was actually not left out, but covered in the
posting. But there are more such details, there always is, e.g.
- For full accuracy the above simplified explanation has to make an
exception for binding the temporary via a return statement (not a
good idea).
- For another example, a class type object ceases to exist (as an
object, not as a region of memory) if you explicitly call the
destructor (not a good idea).
- And for a third example, take the situation where you bind the
temporary to a reference to const member (not a good idea!), like
T(int const& x = 666): m(x){} where x may refer to a temporary.
Cheers,
- Alf