Re: Trivial initialization after non-trivial destruction
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 find the current wording state hard to interpret, but if we consider
the current wording state of
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1116
as representing the committees intention I would say that without
intervening copy of another X object into the storage pointed to by
pointer p, the life-time of the object has not started again, therefore
the second destruction would invoke undefined behaviour. But this also
would mean that the first destruction was invalid, because not object
representation of any X object had ever been copied into the originally
allocated memory.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]