非类型模板参数,并且需要 [英] Non-type template parameters and requires

查看:0
本文介绍了非类型模板参数,并且需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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() {}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆