首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数值类型的类模板

数值类型的类模板
EN

Stack Overflow用户
提问于 2013-01-12 22:28:45
回答 2查看 24.2K关注 0票数 43

如何编写只接受数值类型(intdoublefloat等)的类模板?作为模板?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-12 22:29:39

您可以使用std::is_arithmetic类型的特征。如果只想启用具有此类类型的类的实例化,请将其与std::enable_if结合使用

代码语言:javascript
复制
#include <type_traits>

template<
    typename T, //real type
    typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type
> struct S{};

int main() {
   S<int> s; //compiles
   S<char*> s; //doesn't compile
}

对于更容易使用的enable_if版本,以及免费添加的disable_if,我强烈建议阅读this wonderful article来解决这个问题。

在C++中,上面描述的技术有一个名为“替换失败不是错误”的名称(大多数使用首字母缩写SFINAE)。您可以在wikipediacppreference.com上阅读有关此C++技术的更多信息。

从C++20开始,concepts让这件事变得更容易,而且不会破坏界面:

代码语言:javascript
复制
#include <concepts>

template<typename T>
concept arithmetic = std::integral<T> or std::floating_point<T>;

template<typename T>
  requires arithmetic<T>
struct S{};
// Shorthand: template<arithmetic T> struct S {};

但请注意,有许多用户类型也意味着算术使用,所以在通用接口中,更一般的概念会更好,它涵盖了您正在寻找的操作,而不是您正在寻找的类型。

票数 59
EN

Stack Overflow用户

发布于 2016-05-11 02:05:50

我发现从template<typename T, typename = ...>方法收到的错误消息非常神秘(VS 2015),但发现具有相同类型特征的static_assert也可以工作,并让我指定一个错误消息:

代码语言:javascript
复制
#include <type_traits>

template <typename NumericType>
struct S
{
    static_assert(std::is_arithmetic<NumericType>::value, "NumericType must be numeric");
};

template <typename NumericType>
NumericType add_one(NumericType n)
{
    static_assert(std::is_arithmetic<NumericType>::value, "NumericType must be numeric");
    return n + 1;
}

int main()
{
    S<int> i;
    S<char*> s; //doesn't compile

    add_one(1.f);
    add_one("hi there"); //doesn't compile
}
票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14294267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档