 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Trevor L. Jackson, III Guest
|
Posted: Fri Feb 18, 2005 10:56 pm Post subject: Temporaries versus sequence points |
|
|
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 ]
|
|
| Back to top |
|
 |
Matti Rintala Guest
|
Posted: Sat Feb 19, 2005 7:24 pm Post subject: Re: Temporaries versus sequence points |
|
|
Trevor L. Jackson, III wrote:
| Quote: | 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 ------------ [email]matti.rintala (AT) tut (DOT) fi[/email] ------------
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 ]
|
|
| Back to top |
|
 |
Trevor L. Jackson, III Guest
|
Posted: Sun Feb 20, 2005 12:05 am Post subject: Re: Temporaries versus sequence points |
|
|
Matti Rintala wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|