当我发现 constexpr 递归函数由于评估整个变量范围而始终失败时,我想检查 constexpr 递归函数中的输入模板参数。
示例:
template<unsigned char t>
constexpr unsigned char test() {
static_assert(t < 20, "param check");
return t < 10 ? t : test<t-1>();
}
int main() {
return test<14>();
}
为什么会发生这种情况?还有其他方法可以在编译时检查参数吗?
最佳答案
这是因为test<t>
的实例化始终需要实例化 test<t-1>
,因此您有一个没有停止标准的无限递归。
您可以明确指定停止标准:
template<unsigned char t>
constexpr unsigned char test() {
static_assert(t < 20, "param check");
return t < 10 ? t : test<t-1>();
}
template<>
constexpr unsigned char test<0>() {
return 0;
}
int main() {
return test<14>();
}
另一种方法是使用if constexpr
,那么对于不满足的条件,实例化不会发生:
template<unsigned char t>
constexpr unsigned char test() {
static_assert(t < 20, "param check");
if constexpr ( t < 10 ) {
return t;
} else {
return test<t-1>();
}
}
关于c++ - 如何约束模板化 constexpr 递归函数输入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49213507/