Using C++14. Why will this compile:
template<unsigned N>
constexpr bool foo()
{
std::array<char, N> arr;
return true;
}
but not this?
constexpr bool foo()
{
std::array<char, 10> arr; // Non-constexpr constructor 'array' cannot be used in a constant expression
return true;
}
§7.1.5 [dcl.constexpr]/p6:
It is valid for
constexpr
function templates to have some specializations that do not satisfy theconstexpr
requirements, and it is valid to use those specializations as long as they are not in a context that requires a constant expression.It isn't valid, however, if no specialization of the template could satisfy
constexpr
requirements. Since in the general case it is impossible to determine whether all possible instantiations of a function template will fail to satisfy theconstexpr
requirements,the standard doesn't require a diagnostic. Hence, your code is ill-formed with no diagnostic required - the compiler can, but is not required to, report an error.They don't. Your test is flawed.
The problem is not detected until you actually attempt to instantiate the broken function template.