Non-type template parameters and requires(非类型模板参数,并且需要)
本文介绍了非类型模板参数,并且需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习概念,我想不出约束非类型模板参数的值(非类型)的方法。
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() {}
这篇关于非类型模板参数,并且需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:非类型模板参数,并且需要
猜你喜欢
- 如何提取 __VA_ARGS__? 2022-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- XML Schema 到 C++ 类 2022-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- GDB 不显示函数名 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
