
On Wed, Mar 24, 2010 at 11:29 AM, Robert Jones
On Wed, Mar 24, 2010 at 9:19 AM, Ovanes Markarian
wrote:
Ok, I know I'm having an aberrant moment here, but remind me why a temporary can't be passed as a non-const reference.
Because ISO C++ Standard 2003 prohibits it and explicitly states that the
lifetime of a temporary can be extended when it is bound to a
const-reference. I don't know the exact paragraph, nor have the access to
the Standard right now, but I clearly remember having read it. There is even
an example how long the temporary lives when bound to the const-reference.
The situation will change with the moving semantics and rvalue references in
the upcoming standard C++0x. If you really need to modify the internal
object state you could introduce mutable data members. Those can be changed
from const-qualified member functions. But think twice if you really need
it...
Getting back to the previous example the code looks as:
#include