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.
another. Thus: