C++ Logo

std-proposals

Advanced search

[std-proposals] attribute [[unevaluated]]

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 20 May 2025 08:45:59 +0100
Sometimes we write a function without any intention of it ever being
invoked. Consider the following function to get a function's return
type:

    template <typename ReturnType, typename... Params>
    ReturnType ToReturnType( ReturnType (*)(Params...) )
    {
        // empty function body
    }

The only reason we write a function like this is to use 'decltype' on
its invocation, some thing like:

    typedef decltype( ToReturnType(my_function_pointer) ) ReturnType;

Most compilers will warn that the above function doesn't have a return
statement, so I suppress the warning with:

    template <typename ReturnType, typename... Params>
    ReturnType ToReturnType( ReturnType (*)(Params...) )
    {
        return std::declval<ReturnType>();
    }

It though it might be more intuitive though to mark the function:

    template <typename ReturnType, typename... Params>
    [[unevaluated]] ReturnType ToReturnType( ReturnType (*)(Params...) ) {}

This would have two effects:
    (1) The compiler will refuse to use it in an evaluated context
    (2) The compiler won't give a warning/error about a missing return statement

Received on 2025-05-20 07:46:11