1

How can I check the type of a input variable inside if clause in C++? If there is any member function to do this.

3 Answers3

2

You can try to use:

typeid(yourvariable).name()

You need to include the following header to make it working:

#include <typeinfo>
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

It depends on what type checks you want to do.

The simplest is probably

 #include <typeinfo>     // for the `std::type_info` type

 if (typeid(input_variable) == typeid(chosen_type))
 {
      // input_variable is of type chosen_type
 } 

It is also possible to check the (implementation defined) name strings that identify a type

 if (std::string(typeid(input_variable).name()) == typeid(chosen_type).name())
 {
      // input_variable is of type chosen_type
 } 

The conversion to std::string is needed for comparison operators to work, as the .name() member function returns const char *. Otherwise compare the name() members using strcmp() (either in C's <string.h> or in C++ <cstring> - within namespace std).

Bear in mind that the character sequence returned by typeid(X).name is implementation defined.

In C++11, the type_info type has a hash_code() member, which may also be compared. The values of this are implementation defined, and may vary between executions of the program. Furthermore, as Martin Bonner mentioned in comments, that hash_code() may give false positives for equality (if hash_code()s compare non-equal, the types are different but if they compare equal the types may be different. I mention this, not because I advocate comparing hash_code()s, but because the original question has not explained why comparison of types is desired, so there is no basis to assume a test that might yield false matches is inappropriate.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • 2
    hash_code is not guaranteed to be different for different types. In other words, it's a good value to feed to unordered_map, but not to prove equality (use `typeinfo::operator =()` for that) – Martin Bonner supports Monica May 24 '16 at 10:03
  • As I said, it depends on the type of comparison desired. If `hash_code()` results compare not equal, the types are different. If they compare equal, the types *may* be different, so there is a non-zero likelihood of false matches. Potential comparisons with a likelihood of false positives (or false negatives) are a valid screening technique in some applications. – Peter May 24 '16 at 10:23
  • Exactly! I only commented because the OP might have been under the impression that equality of `hash_code()` proved equal types. – Martin Bonner supports Monica May 24 '16 at 10:33
0

An easy to use solution would be the following one:

#include<cassert>

struct B { static int cnt; };
int B::cnt = 0;

template<class T>
struct S: B { static int type; };

template<typename T>
int S<T>::type = B::cnt++;

template<typename T, typename U>
bool f(T, U) {
    return S<T>::type == S<U>::type;
}

int main() {
    assert(f(42, 0));
    assert(!f(0, .0));
}

You can use S<T>::type in a guard statement or wherever you want.
If you have a variable named x, is a matter of using something like:

S<decltype(x)>::type
skypjack
  • 49,335
  • 19
  • 95
  • 187