[[unevaluated]] auto Func(void)
{
if constexpr ( some_constant0 )
{
return SomeType{};
}
else
{
return SomeOtherType{ 0xFF };
}
}
The idea is that the compiler will forbid the use of Func outside of an unevaluated context.
Why? What's the actual use case? Why is it better than just using a consteval function for the same purpose?
One trick people are using is to put this in the body:
static_assert(1 == 2, "Func must only be used in an unevaluated context");
That can't even be compiled, even if it's never used in any context, evaluated or not.
But I think we'd be better off with an attribute that tells the compiler exactly what we have in mind. And of course the attribute would ensure that the function is not emitted in the object file.
You get that with consteval.