Talk:cpp/memory/destroy n
From cppreference.com
< Talk:cpp
[edit] Undefined behavior in example
The example on this page contains undefined behavior. When std::destroy_n is called, "ptr" points only to a single object of type Tracer. This is because each Tracer object was created individually, and the Standard's definition of pointer arithmetic ([expr.add]/4), does not account for the possibility of multiple objects existing in adjacent storage.
The way that the Standard is now, only calling placement array new can construct on top of storage in a way that destroy_n can legally destruct more than one object.
The current Standard is rather broken in this regard, because it implies std::vector can't be reimplemented within the language itself. See Richard Smith's proposed fix, P0593R2 [1].