The output in my platform (Windows 7 Ultimate 64 bits Service Pack 1 in a x86 PC compatible machine with an AMD Phenom II 1090T X6 3.2Ghz microprocessor; source code compiled with Microsoft Visual C++ Express IDE) is:
f()
C(), i = 1
h()
~C(), i = 1
END
Here, the temporal object C returned by function f() still lives when function h() is called and is destroyed inmediately after function h() returns to his caller (the function main()). So, it seems that a returned temporal object lives while it is used and it is destroyed when not used (in the next sentence of the sentence that call the function that returns the temporal object). Does the C++ standard specify that this must be the behaviour of C++ compilers?
What does the C++ standard say about returned temporal objetcts's lifetime ?
12.3[class.temporary]/3-5, it's fairly long, but the first point is the one that applies here:
iso wrote:
Temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created.
DavidIRE wrote:
C returned by function f() still lives when function h() is called and is destroyed inmediately after function h() returns to his caller
The function call to f() is a part of the full-expression h(f()), so the temporary that is created when f() returns by value is destroyed after the function call to h() ends.