[Issue 23973] static constructors should have to be nothrow

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 13 18:45:42 UTC 2023


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

Jonathan M Davis <issues.dlang at jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang at jmdavisProg.co
                   |                            |m

--- Comment #4 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
(In reply to RazvanN from comment #2)
> Would make sense to apply the same constraints for static destructors?
> Actually, I think that that would make sense for constructors and
> destructors in general.

Constructors in general are a very different issue from static constructors.
Throwing from constructors on types is an extremely normal thing to do. Without
that, you're often forced to do two step initialization in order to be able to
tell the caller that the arguments are invalid. Disallowing throwing from
normal constructors would very much be an anti-pattern.

It _might_ make sense to require that destructors on types be nothrow, but I'm
not sure. C++ went and made noexcept the default for destructors, and their
standard library doesn't allow destructors to throw exceptions, but IIRC, some
projects actually do it. So, as I understand it, the C++ community considers it
something that should generally be avoided but not necessarily something that
should absolutely never be done. So, I think that we'd have to look at the
situation very carefully before disallowing it in D.

Regardless, the question of static constructors and destructors and nothrow is
very different from non-static ones, and the issues that Atila brought up with
static constructors do not apply to regular constructors at all outside of them
being called from static constructors - and those issues then apply to any code
that would throw an exception from within a static constructor.

--


More information about the Digitalmars-d-bugs mailing list