[Issue 22177] emplace should handle throwing constructors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 27 08:36:52 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22177

Stanislav Blinov <stanislav.blinov at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |stanislav.blinov at gmail.com

--- Comment #3 from Stanislav Blinov <stanislav.blinov at gmail.com> ---
I think the language should specify (as in, explicitly state in the spec) the
expected state of an object in case its ctor throws. IMO it should be the
user's job to leave their objects in a destructible state.

Perhaps something like: "If a constructor throws an exception, the object MAY
still be destructed. Therefore, constructors should always leave objects in a
destructible state."

Pseudo-code for emplace is:

emplaceInitializer(ptr);
constructAt(ptr, args);

Thus it makes little sense to write the initializer again. User was given a
piece of memory, it's theirs now to keep tidy. It follows then that it should
be the user's responsibility to maintain exception safety in their ctors.

An outlier here is emplacement of arrays, where it does make sense to write
.init into the remainder of array if an exception was thrown, as that *is*
assumed to contain garbage state.

Overwriting partially-constructed state with .init can potentially be
problematic sine can cause resource leaks.

--


More information about the Digitalmars-d-bugs mailing list