非类型模板参数,并且需要
我正在学习概念,我想不出约束非类型模板参数的值(非类型)的方法。
Example编译的代码,尽管我希望它不会(由于失败的要求):
#include <cassert>
enum Bla{
Lol,
Haha
};
template<Bla b>
requires requires{
// my guess is that this just checks that this is valid expression, not
// that it is true
b>1;
}
void f(){
assert(b>1);
}
int main() {
f<Lol>(); // compiles, not funny ;)
}
注意:
这是一个简化的示例(我想要模板重载),因此static_assert
对我不好,我正在努力避免std::enable_if
,因为语法很难看。
解决方案
如果只有布尔条件,没有其他条件,请执行以下操作:
template<Bla b>
requires(b > 1)
void f() {}
替代更长的语法,如果您需要在同一requires
-表达式中检查更多内容:
template<Bla b>
requires requires
{
requires b > 1;
// ^~~~~~~~
}
void f() {}
相关文章