如何使用概念将类型名限制为数字? C++20 特性

问题描述

我正在学习一些现代 C++20 特性。其中之一是概念。我已阅读 https://en.cppreference.com/w/cpp/language/constraintshttps://www.modernescpp.com/index.php/defintion-of-concepts 以获取一些示例。

现在,我想设计一个概念,以便我只能接受数字数据类型。在传统方法中,可以使用

template<typename NumericType,typename = typename std::enable_if<std::is_arithmetic<NumericType>::value,NumericType>::type>

Class template for numeric types中的建议

或者也可以在模板类/函数的定义中使用static_assert()

static_assert(std::is_arithmetic<NumericType>::value,"NumericType must be numeric");

我想知道概念的语法应该是什么?现在,我正在做

template<typename NumericType>
concept Numeric = std::is_arithmetic<NumericType>::value ;

然后找到

template<Numeric T>
void f(T){}

可以达到我预期的效果(基本上只是鸭子打字)。我想知道我是正确的还是有什么区别?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)