非类型模板参数,并且需要

我正在学习概念,我想不出约束非类型模板参数的值(非类型)的方法。

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,因为语法很难看。

asdfg123321123321 回答:非类型模板参数,并且需要

如果只有布尔条件,没有其他条件,请执行以下操作:

template<Bla b>
requires(b > 1)
void f() {}

替代更长的语法,如果您需要在同一requires-表达式中检查更多内容:

template<Bla b>
requires requires
{
    requires b > 1;
//  ^~~~~~~~
}
void f() {}

这篇关于非类型模板参数,并且需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!

本文链接:https://www.f2er.com/3179414.html

大家都在问