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.");
}
Since C++ does not have reflection, the answer is no: it is not possible.
What I, instead, suggest you to do is to make a different design choice in order to achieve the same goal. You could use, for example, polymorfism in an run-time environment.
If you need some at compile time, instead, you can "integrate" a constant expression in your structure and check it later.
For example:
struct AbortError {
static constexpr bool critical = true;
// ...
};
or
struct AbortError: std::true_type {
// ...
};
Here an online example which shows both options.