考虑以下代码片段:
#include <limits>
#include <stdexcept>
void g(unsigned) {
// ...
}
template<typename UIntT>
void f(UIntT n)
{
if constexpr (std::numeric_limits<UIntT>::max() > std::numeric_limits<unsigned>::max())
{
if (n > std::numeric_limits<unsigned>::max())
throw std::length_error("Too long.");
}
g(n);
}
我想知道“if constexpr”子句在这里是否真的有用。编译器是否不够聪明,无法确定给定 UIntT 的“if”子句是否为真?如果是这样,这是标准规定的吗?