如何编写只接受数值类型(int、double、float等)的类模板?作为模板?
发布于 2013-01-12 22:29:39
您可以使用std::is_arithmetic类型的特征。如果只想启用具有此类类型的类的实例化,请将其与std::enable_if结合使用
#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)。您可以在wikipedia或cppreference.com上阅读有关此C++技术的更多信息。
从C++20开始,concepts让这件事变得更容易,而且不会破坏界面:
#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 {};但请注意,有许多用户类型也意味着算术使用,所以在通用接口中,更一般的概念会更好,它涵盖了您正在寻找的操作,而不是您正在寻找的类型。
发布于 2016-05-11 02:05:50
我发现从template<typename T, typename = ...>方法收到的错误消息非常神秘(VS 2015),但发现具有相同类型特征的static_assert也可以工作,并让我指定一个错误消息:
#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
}https://stackoverflow.com/questions/14294267
复制相似问题