C++ Logo

std-proposals

Advanced search

Re: [std-proposals] noexcept has gotten a bit hairy -- I want a compiler error

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 29 Apr 2025 01:26:42 +0100
On Sun, Apr 27, 2025 at 5:55 AM Jan Schultke wrote:
>
> The issue with the proposal is that many functions exist that don't throw but aren't marked noexcept


I wonder should we have "std::wont_throw" for this purpose? Something
like the following:

    #include <cstring> // strlen
    #include <utility> // forward
    #include <type_traits> // decay, is_function, remove_cvref, remove_pointer

    namespace std {

        template<typename T>
        struct make_function_ptr_noexcept {};

        template<bool b, typename R, typename... Params>
        struct make_function_ptr_noexcept< R(*)(Params...) noexcept(b) > {
            using type = R(*)(Params...) noexcept;
        };

        // The following function can accept either a
        // "reference to a function" or a "pointer to a function"
        template<typename FuncParam, typename... Params>
        requires is_function_v< remove_pointer_t< remove_cvref_t<FuncParam> > >
        decltype(auto) wont_throw( FuncParam &&pf, Params&&... args ) noexcept
        {
            typedef decay_t<FuncParam> FuncPtr; // always yields a
function pointer

            typedef typename make_function_ptr_noexcept<FuncPtr>::type
FuncPtrNoExcept;

            FuncPtrNoExcept const pf_noexcept =
static_cast<FuncPtrNoExcept>(static_cast<FuncPtr>(pf));

            return pf_noexcept( forward<Params>(args)... );
        }
    }

    int main(int const argc, char **const argv)
    {
        return std::wont_throw( std::strlen, argv[0] );
    }

Received on 2025-04-29 00:26:49