Check if a variable is numeric or string

Asked

Viewed 2,339 times

2

Good afternoon, Is there a function in C++ that checks whether the variable is a string or numeric?

An example is: In PHP use is_string(variable) or is_numeric(variable).

  • I don’t know if there is a native function. But there are implementations like this one: http://stackoverflow.com/a/4654718/3293600

  • You want to check whether the type of the variable itself, within, for example, a template function, or you have a string and want to validate whether its content is number?

1 answer

2


If you are using GCC you can use typeof. It returns a string with the type of the variable. Ex.:

#include <iostream>

std::cout << typeof(int&) << '\n'; // int

You can also use type ID. So:

#include <typeinfo>

// …
std::cout << typeid(a).name() << '\n';

Has that question here in Stackoverflow which explains the difference between the 2.

Browser other questions tagged

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