A related question provides the example of a type-independent false
in a static_assert
:
template<class T> void foo()
{
if constexpr(false)
static_assert(false);
}
However, I am more concerned if the same thing applies to a type-dependent false
. Here is the relevant quote from the standard:
The program is ill-formed, no diagnostic required, if no valid specialization can be generated for a template or a substatement of a constexpr if statement within a template and the template is not instantiated. § 13.7/8.1
This comes as a surprise to me as I frequently see the following idiom:
template<class T> void foo()
{
if constexpr(cond)
// ...
else
static_assert(!std::is_same_v<T, T>);
}
In fact, cppreference even provides an example of the same thing:
template<class T> struct dependent_false : std::false_type {};
template<class T> void foo()
{
if constexpr (cond)
// ...
else
static_assert(dependent_false<T>::value);
}
From my understanding, in both of these cases, no valid specialization can ever be generated for the relevant if constexpr
substatements, and are therefore ill-formed, no diagnostic required. Am I correct?