Providing a static_assert
in templates are often helpful. In the case where a template shouldn't be instantiated in a certain way at all, I often do this
template<typename T, typename = void>
struct S
{
static_assert(false, "Unconditional error");
static_assert(sizeof(T) != sizeof(T), "Error on instantiation");
};
template<typename T>
struct S<T, std::enable_if_t<std::is_integral_v<T>>>
{
// ...
};
The first static_assert
will fail instantly, even without an instantiation of S
, while the second will succeed if no instantiations will result in the primary template.
The second static_assert
is obviously a tautology, but it "depends" on T
such that the intended effect is achieved. But is this guaranteed? Are compilers allowed to evaluate these tautologies?