Re: Placement new and explicit destruction
On Jul 16, 11:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
REH wrote:
This curiosity popped into my head on the way to work today. I
wouldn't actually do this, but just wondering. Is the following
defined behavior?
#include <new>
class T { };
int main()
{
T* p = new T();
p->~T();
new(p) T();
delete p;
return 0;
}
Yes. It's the pattern some folks are using to "forward constructor
calls". The functionality is so often asked for that the Committee
is changing the language to allow forwarding constructors, BTW.
It's definitly legal, but I don't quite see what it has to do
with forwarding constructors. If I've understood the proposals
correctly, a forwarding constructor is where one constructor
first calls another constructor to do the job, then does some
additional processing. Here, the poster first allocates and
constructs the object, then destructs it without deallocating
(leaving raw memory), then reconstructs it in the raw memory.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34