Is the order of execution of the three commented lines below guaranteed?
struct S
{
S() { /* called 1st */ }
~S() { /* called 3rd */ }
};
boost::shared_ptr<S> f()
{
return boost::shared_ptr<S>(new S);
}
int second() { return 0; /* called 2nd */ }
int test()
{
return (f(), second());
}
With my compiler, the shared_ptr
returned by f()
seems to persist until after second()
is called. But is this guaranteed by the standard and thus other compilers?
Yes.
Temporaries persist until the completion of the
full-expression
.And:
This means that both
f()
andsecond()
should exist until execution returns fromtest()
with the result of evaluating the latter.