How to verify if a type is numerical in C++?

Asked

Viewed 140 times

5

Let’s assume 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;
}

You can tell if the guy T is numerical?

  • I think you’ve got it all https://rosettacode.org/wiki/Determine_if_a_string_is_numeric

2 answers

6


If you are using an older version of C+= you have to resort to some tricks to get the expected result. In the OR there’s an answer with one of them. Has other sources that indicate tricks. Or you can do so:

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

If you are already using C++ 17 and need to do more than one assert. has something in the language that helps a lot that is the 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");
}

I put in the Github for future reference.

And finally if you can wait for C++ 20 you can make use of Concepts which is the ultimate solution. The above options are palliative because the language cannot express in code restrictions on the use of the template variable directly.

  • I’m using c++ 17

4

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

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

Exit:

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;
}
  • Thank you for teaching me about Std::boolalpha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.