Namespaces
Variants
Views
Actions

Talk:cpp/memory/destroy n

From cppreference.com

[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].

--Myria (talk) 12:42, 7 December 2018 (PST)

Yes. But I'm going to keep the example as it is for this particular case of UB unless someone can show me an implementation that does the wrong thing. T. Canens (talk) 16:43, 7 December 2018 (PST)