C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Mark a class or an object as 'noexcept'

From: Howard Hinnant <howard.hinnant_at_[hidden]>
Date: Wed, 28 May 2025 10:12:24 -0400
On May 28, 2025, at 5:36 AM, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> There are times when I'm writing template code and I end up with
> 'noexcept' specifications something like:
>
> template<typename T>
> void Func(T&&) noexcept( noexcept( std::declval<T&>.clear() ) &&
> noexcept(std::declval<T&>.some_other_method() ) );
>
> when what I really want is that 'T' would be some sort of class whose
> methods never throw.
>
> Would it be helpful at all to be able to mark a class or an individual
> object as 'noexcept'? Maybe something like:
>
> class noexcept MyClass { . . . };
>
> or maybe even mark an object:
>
> MyClass noexcept my_object;
>
> so that we get a compiler error if any of the class's methods are not
> marked as 'noexcept'?
>
> (I think marking the class would be a lot less work, as marking an
> object I think would mean adding a cv-qualifier to the language)

Sometimes part of a type’s API is a member function, and sometimes a namespace scope function. E.g.: operator==.

I’m concerned that if a type is marked noexcept that it might raise confusion for such namespace scope functions. They may be assumed to be part of the API of a type, but not be implacted by this type’s noexcept qualification.

Howard

Received on 2025-05-28 14:12:57