https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86908

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Kostya Frumkin from comment #8)
> At the same time this design pattern (strategy) with placement-new is more
> efficient than with common new.

That's fine, but you can't use &strategy to access the AStrategy object you
created at that position. You have several choices:

- use an untyped buffer and create objects in there:

  alignas(AStrategy) unsigned char buf[sizeof(AStrategy)];

- use std::launder

- use the pointer returned by the placement new-expression, which has the right
type


There are ways to do this safely in C++ today, but your original code is not
one of those ways. You should fix your code.


> So it has no rationale if the base methods
> are used when using placement-new. At the same time other compilers
> generates code where the derived method is callsed after placement-new. 

It's undefined behaviour, so anything can happen.

Reply via email to