Re: Trivial initialization after non-trivial destruction
Am 11.05.2012 01:18, schrieb Johannes Schaub:
Am 10.05.2012 20:48, schrieb Nikolay Ivchenkov:
Consider the following example:
struct X
{
~X() {}
};
template<class T>
void destroy(T&x)
{ x.~T(); }
int main()
{
X *p = (X *)operator new(sizeof(X));
destroy(*p);
destroy(*p); // well-defined or undefined?
operator delete(p);
}
According to C++11 - 3.8/1, non-trivial destruction ends the
life-time of an object. Can we assume that a new object of the same
type exists at the same location immediately after such non-trivial
destruction has done if its initialization is trivial?
I lately came to the following conclusions (they are by no means
"normative". All of this is by the necessity of the spec being way
too unspecific IMO):
- The start of lifetime of an object of trivial initialization is
the same as the start of existence of that object (it may be "out of
lifetime". It is during its ctor run, and before it. In the latter
case, it's almost unusable except for the non-value uses).
- The start of lifetime of other objects equals the start of
lifetime of them. The existence of the object is implied by its
start of lifetime. It's the "created by the implementation when
needed" case of 1.8p1, despite the "weird" cross reference :)
I am sorry for not being awake when I wrote that text. What I wanted
to say in those two bullets:
- The start of lifetime of an object with trivial initialization is
the same as the start of existence of that object. If the object has
no explicit begin of existence (by a definition, new-expression or
temporary expression like "string()"), the existence of the object is
implied by the start of lifetime ("created by the implementation when
needed", 1.8p1).
- The start of lifetime of other objects may happen after the object
started existence (applies to class types during their ctor. lifetime
starts when the ctor finishes execution successfully).
I hope I could clarify my view on it! See my other post for examples
of "implied start of existence". I think one of them is doing a write
access by an lvalue of type "X", starting existence of an object of
type X if X has a trivial default constructor and there is no object
already existent of type "X" at its location (diregarding
cv-qualifiers) (example: doing a write with an lvalue of type
"volatile int" to an object of type "int" will not create an object of
type "volatile int" / will not destroy the "int" object).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]