How to check if a type is numeric in C ++?

5

Let's suppose I have a function:

template<class T>
T soma(const T& x, const T& y) {
    static_assert(/*O tipo é for numérico?*/, "Tipo de argumento inválido, O argumento precisa ser numérico");
    return x + y;
}

Can you tell if the type T is numeric?

    
asked by anonymous 09.08.2018 / 23:27

2 answers

6

If you are using an older version of C + = you have to resort to a few tricks to get the expected result. In the SO has an answer with one of them. You have other fonts that indicate tricks. Or you can do this:

static_assert(is_arithmetic_v<T>::value, "Must be arithmetic");

If you are already using C ++ 17 and need to make more than assert . there is something in the language that helps a lot that is if constrexpr . Example:

template<class T> struct dependent_false : std::false_type {};
template<class T>
T soma(const T& x, const T& y) {
    if constexpr (std::is_arithmetic_v<T>)
        return x + y;
    else
        // faz alguma coisa aqui
        static_assert(dependent_false<T>::value, "Must be arithmetic");
}

And finally, if you can wait for C ++ 20 you can use Concepts which is the ultimate solution. The above options are palliative because the language can not code-constrain the use of the directly lined variable.

    
09.08.2018 / 23:41
5

From C++11 you can use the std::is_arithmetic function of the default library type_traits to check whether an arbitrary type is an integer or a real number (floating point), see :

#include <iostream>
#include <type_traits>

class A {};

int main() 
{
    std::cout << std::boolalpha;
    std::cout << "A:           " <<  std::is_arithmetic<A>::value << '\n';
    std::cout << "bool:        " <<  std::is_arithmetic<bool>::value << '\n';
    std::cout << "int:         " <<  std::is_arithmetic<int>::value << '\n';
    std::cout << "int const:   " <<  std::is_arithmetic<int const>::value << '\n';
    std::cout << "int &:       " <<  std::is_arithmetic<int&>::value << '\n';
    std::cout << "int *:       " <<  std::is_arithmetic<int*>::value << '\n';
    std::cout << "float:       " <<  std::is_arithmetic<float>::value << '\n';
    std::cout << "float const: " <<  std::is_arithmetic<float const>::value << '\n';
    std::cout << "float &:     " <<  std::is_arithmetic<float&>::value << '\n';
    std::cout << "float *:     " <<  std::is_arithmetic<float*>::value << '\n';
    std::cout << "char:        " <<  std::is_arithmetic<char>::value << '\n';
    std::cout << "char const:  " <<  std::is_arithmetic<char const>::value << '\n';
    std::cout << "char &:      " <<  std::is_arithmetic<char&>::value << '\n';
    std::cout << "char *:      " <<  std::is_arithmetic<char*>::value << '\n';
}

Output:

A:           false
bool:        true
int:         true
int const:   true
int &:       false
int *:       false
float:       true
float const: true
float &:     false
float *:     false
char:        true
char const:  true
char &:      false
char *:      false

In your case:

#include <type_traits>

template<class T>
T soma(const T& x, const T& y) {
    static_assert( std::is_arithmetic<T>::value, "Tipo de argumento inválido, O argumento precisa ser numérico");
    return x + y;
}
    
09.08.2018 / 23:40