What does nothrow buy me?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Oct 26 19:09:52 UTC 2020


On Mon, Oct 26, 2020 at 06:52:40PM +0000, Tobias Pankrath via Digitalmars-d wrote:
> On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:
> > Obviously, nothrow documents the behavior of a function and is
> > checked when the code is available to the compiler.
> > 
> > At first glance, one could assume a nothrow function can save the
> > compiler from the need to generate exception handling stuff for a
> > function. But since nothrow doesn't mean the function won't throw,
> > but merely that it won't throw an Exception, what optimizations does
> > nothrow enable?
> 
> Isn't proper unwinding of the stack only required for Exceptions?

Yes.  The compiler is permitted to skip stack unwinding code when
throwing Errors.  So yes, nothrow does win you some optimizations in
some cases.

That's one of the reasons user code generally shouldn't catch Errors --
because some unwinding code may have been skipped and your state may be
inconsistent.  Continuing normal execution after catching an Error is
definitely a no-no.


T

-- 
Life is too short to run proprietary software. -- Bdale Garbee


More information about the Digitalmars-d mailing list