How to get the type of a variable?

In C ++, how to find the type of a variable?

+75
c ++ variables typeof
Jul 03 2018-12-12T00:
source share
8 answers

You can use the typeid operator :

#include <typeinfo> ... cout << typeid(variable).name() << endl; 
+94
Jul 03 '12 at 12:30
source share

For static statements, decltype introduced in C ++ 11, which is very useful in certain scenarios.

+14
Aug 08 '16 at 16:14
source share

If you have a variable

 int k; 

You can get its type using

 cout << typeid(k).name() << endl; 

See the following thread on SO: A similar question

+9
Jul 03 2018-12-12T00:
source share

Usually, finding a variable type in C ++ is the wrong question. This is usually what you carry with procedural languages ​​like C or Pascal.

If you want to encode different types of behavior depending on the type, try to find out, for example. function overloading and object inheritance . It will not make direct sense on the first day of working in C ++, but hold on to it.

+9
Jul 03 2018-12-12T00:
source share

The main difference between C ++ and Javascript is that C ++ is a language with a typed type, wile javascript is dynamic.

In dynamically typed languages, a variable can contain any thing, and its type is determined by the value that it has, moment by moment. In static typed languages, the type of a variable is declared and cannot be changed.

There may be a dynamic layout of the send and the object, as well as subtyping (inheritance and virtual functions), as well as static mailing and supertyping (via the CRTP template), but in any case, the type of the variable must be known to the compiler.

If you are able to not know what it is or maybe, it is because you have developed something because the language has a dynamic type system.

If in this case you better think about your design, because it falls into the ground, which is not natural for the language you use (most of all, for example, on a highway with a caterpillar or in the water with a car)

+6
Jul 03 2018-12-12T00:
source share

I believe that I have a valid use case for using typeid (), just as it is true for using sizeof (). For the template function, I need special code based on the template variable, so I offer maximum functionality and flexibility.

It is much more compact and convenient than using polymorphism to create one instance of a function for each supported type. Even so, I could use this trick to write the function body only once:

Note that since the code uses patterns, the switch statement below should only be statically placed in one block of code, optimizing all the false cases, AFAIK.

Consider this example, where we may need to handle the conversion if T is one type vs another. I use it to specialize a class for accessing hardware, where the hardware will use either myClassA or myClassB. In the event of a mismatch, I need to spend time converting the data.

 switch ((typeid(T)) { case typeid(myClassA): // handle that case break; case typeid(myClassB): // handle that case break; case typeid(uint32_t): // handle that case break; default: // handle that case } 
+3
May 25 '16 at
source share

I am not sure if my answer will help.

Short answer: you do not need / do not need to know the type of the variable in order to use it.

If you need to give the type of a static variable, then you can just use auto.

In the more complex case, when you want to use "auto" in a class or structure, I would suggest using a template with decltype.

For example, suppose you use a foreign library that has an unknown_var variable in it, and you want to put it in a vector or structure, you can do this completely:

 template <typename T> struct my_struct { int some_field; T my_data; }; vector<decltype(unknown_var)> complex_vector; vector<my_struct<decltype(unknown_var)> > simple_vector 

Hope this helps.

EDIT: For convenience, here is the most difficult case I can think of: having a global variable of unknown type. In this case, you will need C ++ 14 and a template variable.

Something like that:

 template<typename T> vector<T> global_var; void random_func (auto unknown_var) { global_var<decltype(unknown_var)>.push_back(unknown_var); } 

It's still a little tedious, but it's as close as you can get to typical languages. Just make sure that when you reference a template variable, always put the template specification there.

+3
Jan 03 '18 at 4:03
source share
 #include <typeinfo> ... string s = typeid(YourClass).name() 
+2
Jul 03 2018-12-12T00:
source share



All Articles