C++ Logo

std-proposals

Advanced search

Re: [std-proposals] noexcept(static)

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Tue, 15 Sep 2026 15:44:33 +0200
The Lakos rule states that functions with a narrow contract (preconditions / requirements) should not be marked as noexcept. Violation of such preconditions lead to unexpected behavior.   As far as I understand it: A standard library implementation in some safety-mode could check those preconditions and create exceptions, if they are not fulfilled. Then specifically on this implementation *not* full UB happens, but a deterministic error handling: exceptions are thrown. As due to the Lakos rule those functions are not marked as noexcept, the throwing of the exceptions is acceptable behavior within or at least compatible with the standard: We violated the standard with the not fulfilled pre-conditions, but this specific implementation reacted in a deterministic standard-compliant way.   -----Ursprüngliche Nachricht----- Von:Rainer Deyke via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Di 15.09.2026 14:22 Betreff:Re: [std-proposals] noexcept(static) An:std-proposals_at_[hidden]; CC:Rainer Deyke <rainerd_at_[hidden]>; On 9/15/26 14:01, Frederick Virchanza Gotham via Std-Proposals wrote: > On Mon, Sep 7, 2026 at 1:59 PM Thiago Macieira wrote: > I want to go back to first principles of what I intended with > 'noexcept(static)'. Most basically, where we have code like this: > >      void Func(void) >      { >          a(); >          b(); >          c(); >      } > > I want it to be compiled as follows: > >      void Func(void) >      { >          static_assert( noexcept( a() ) ); >          a(); >          static_assert( noexcept( b() ) ); >          b(); >          static_assert( noexcept( c() ) ); >          c(); >      } > > What complicates this significantly, as you mentioned, is the Lakos > rule -- i.e. "The throwing of an exception is an acceptable form of > undefined behaviour". But given the Lakos rule, should "std::sqrt" be > defined as follows? > >      double sqrt(double) noexceptLakos; If a program invokes undefined behavior, then *all* guarantees are off, including the guarantee that no exception can leave a noexcept function.  Therefore there is no distinction from the user side between your proposed noexceptLakos and plain old noexcept. On the implementation side, there is currently no *standard* way to say "this exception is allowed to leave this noexcept function, because it is only thrown when undefined behavior is invoked".  However, C++ implementations are free to create a non-standard extension that does just that. -- Rainer Deyke - rainerd_at_[hidden] -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-09-15 13:51:22