ma**********@comcast.net wrote:
Hi,
According to ISO C++ (7.11/2), "An object declared without a
storage-class-specifier at block scope or declared as a function
parameter has automatic storage duration by default."
What about temporary objects used as default function arguments?
Default arguments (the '= blah' syntax) is just another way to
initialise the argument (just as if you passed that expression in
when making the call).
If I have:
class A{};
void g( A &b = A() ){}
what is the storage duration of the temporary?
Your code is ill-formed. A reference to non-const cannot be
initialised with (bound to) a temporary. Change 'b' to be a ref
to 'const A', then your code would be OK.
The semantics (and the lifetime of the temporaries) is described
in [class.temporary]/5, they even have an example that is almost
like your code here. In short: it survives any call to 'g'. The
precise moment of that temporary's destruction cannot be determined
without seeing the code that calls 'g'.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask