Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do I write a class template that accepts only numeric types (int, double, float, etc.) as template?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
120 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use the std::is_arithmetic type trait. If you want to only enable instantiation of a class with such a type, use it in conjunction with 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
}

For a version of enable_if that's easier to use, and a free addition of disable_if, I highly recommend reading this wonderful article on the matter.

In C++, the technique described above has a name called "Substitution Failure Is Not An Error" (most use the acronym SFINAE). You can read more about this C++ technique on wikipedia or cppreference.com.

As of C++20, concepts make this much easier and don't spoil the interface:

#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 {};

Do note that there are many user types meant to be used arithmetically as well, though, so a more general concept that covers the operations you're looking for instead of the types you're looking for would be preferable in a generic interface.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...