Is it possible to test for an attribute specifier in C++?

Viewed 98

I'm trying to achieve different behavior based on an attribute applied to a function. Basically I'm trying to see if it is possible to test for the presence of an attribute at compile time.

struct AbortError
{
    [[noreturn]] static void error(const std::string& msg)
    {
        std::cerr << "critical error: " << msg << std::endl;
        std::abort();
    }
}

struct LogError
{
    static void error(const std::string& msg)
    {
        std::cerr << "non-critical error: " << msg << std::endl;
    }
}

Is it possible to test for the presence of [[noreturn]]?

I'm hoping to acheive something like

template <typename ErrorPolicy>
void function()
{
    // ...
    if constexpr(std::is_noreturn<ErrorPolicy::error>::value)
    {
        performCriticalCleanup();
    }

    ErrorPolicy::error("Bad things happened.");
}
1 Answers
Related