Topic: Temporaries versus sequence points


Author: tlj3@comcast.net ("Trevor L. Jackson, III")
Date: Fri, 18 Feb 2005 22:56:36 GMT
Raw View
Given an object whose initialization requires a special machine state,
and which machine state has to be restored once the object's constructor
has completed, I am tempted to use a temporary in the constructor
initialization list.

For example:
 struct Tmp { // Manage machine state
   state_t state_;
   Tmp() : state_( true ) {}
  ~Tmp() { state_ = false; }
 }
 struct Object { // Depends in special machine state
  data_t data_;
  Object : data_( Tmp(), /* init_expr goes here*/ ) {}
 }

However, if the temporary Tmp is not certain to survive past the comma
then I have to eliminate the sequence point at some cost in clarity.

Does the temporary Tmp in the initialization list of the Object
constructor last until the end of the full expression (the closing
parenthesis) or only until the next sequence point (the comma operator)?

/tj3

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: matti.rintala@tut.fi (Matti Rintala)
Date: Sat, 19 Feb 2005 19:24:40 GMT
Raw View
Trevor L. Jackson, III wrote:
>     struct Tmp {    // Manage machine state
>          state_t state_;
>          Tmp() : state_( true ) {}
>         ~Tmp() { state_ = false; }
>     }
>     struct Object {    // Depends in special machine state
>         data_t data_;
>         Object : data_( Tmp(), /* init_expr goes here*/ ) {}
>     }
> Does the temporary Tmp in the initialization list of the Object
> constructor last until the end of the full expression (the closing
> parenthesis) or only until the next sequence point (the comma operator)?

Yes, it survives:

12.2 3: "Temporary objects are destroyed as the last step in evaluating
the full-expression (1.9) that (lexically) contains the point where they
were created."

Here a full expression is the whole comma expression, so the temporary
object is destroyed at the end of it.

--
------------- Matti Rintala ------------ matti.rintala@tut.fi ------------
Painting is the art of inclusion. Photography is an art of exclusion.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: tlj3@comcast.net ("Trevor L. Jackson, III")
Date: Sun, 20 Feb 2005 00:05:44 GMT
Raw View
Matti Rintala wrote:

> Trevor L. Jackson, III wrote:
>
>>     struct Tmp {    // Manage machine state
>>          state_t state_;
>>          Tmp() : state_( true ) {}
>>         ~Tmp() { state_ = false; }
>>     }
>>     struct Object {    // Depends in special machine state
>>         data_t data_;
>>         Object : data_( Tmp(), /* init_expr goes here*/ ) {}
>>     }
>> Does the temporary Tmp in the initialization list of the Object
>> constructor last until the end of the full expression (the closing
>> parenthesis) or only until the next sequence point (the comma operator)?
>
>
> Yes, it survives:
>
> 12.2 3: "Temporary objects are destroyed as the last step in evaluating
> the full-expression (1.9) that (lexically) contains the point where they
> were created."
>
> Here a full expression is the whole comma expression, so the temporary
> object is destroyed at the end of it.

Thanks for the info.

/tj3

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]