Re: Is it *legal* (as opposed to sensible) to explicitly destruct an object and deallocate the memory?
"Stuart Golodetz" <blah@blah.com> a ?crit dans le message de news:
ie2r1k$18f$1@speranza.aioe.org...
Hi all,
Just feeling curious -- I know the following is ill-advised, but is it
actually formally illegal?
#include <iostream>
struct X
{
~X()
{
std::cout << "~X()\n";
}
};
int main()
{
X *x = new X;
//delete x;
x->~X();
::operator delete(x);
return 0;
}
It would certainly be legal in the context of *placement* new, but is
there a requirement in the standard that all "new"s are matched by
"delete"s, rather than "operator delete"s?
Cheers,
Stu
destructing "by hand" that way is particularly useful when
constructing/destructing objects cannot use standard new/delete operators,
for example to implement classes like the "any" concept and using some
memory place over time to store different objects of classes A then B then
C.
in these constructions the 'placement' new is as well generally used (i.e.
new (x_ptr) X; )
Regards
--
Armel Asselin