Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


#ActualiMalc

Posted 06 April 2013 - 05:03 PM

The problem isn't throwing the object. The problem is modifying the temporary object, which (by standard C++) is undefined behavior. The problem is binding a non-constant reference to a temporary object and then modifying the object (not throwing the object). In the ThrowException() function, e is a reference to a temporary, and you are modifying it, which is undefined behavior. e should be a const reference, and you should not modify it (feel free to throw it).

Is not the reference valid for the lifetime of the temporary object?  I was certain pg.245 of N3337 implies that the temporary object the reference was bound to will exist for the duration of the function, in particular 12.2.5.  Am I reading this wrong?
Nope, it's not. A const-reference extends the lifetime of a temporary object, a non-const-reference does not.
See GotW #88:
http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/

#1iMalc

Posted 06 April 2013 - 05:03 PM

The problem isn't throwing the object. The problem is modifying the temporary object, which (by standard C++) is undefined behavior. The problem is binding a non-constant reference to a temporary object and then modifying the object (not throwing the object). In the ThrowException() function, e is a reference to a temporary, and you are modifying it, which is undefined behavior. e should be a const reference, and you should not modify it (feel free to throw it).

Is not the reference valid for the lifetime of the temporary object?  I was certain pg.245 of N3337 implies that the temporary object the reference was bound to will exist for the duration of the function, in particular 12.2.5.  Am I reading this wrong?
Nope, it's not. A const-reference extends the lifetime of a temporary object, a non-const-reference does not.

http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/

PARTNERS