0

Is there is a functions that tells you what kind of you data you're dealing with in C++?

In python there exist the type() function but is there is one in C++?

x = 20
>>> type(x)
<class 'int'>
  • It's been a long time since I used c++, but as I recall, you have to explicitly say the type of the variable when it's declared, for example `int x;`. So in most (nearly all?) cases, you would have no need to discover the type at runtime. – John Gordon Dec 04 '22 at 23:14

1 Answers1

0

The most direct equivalent is typeid(var).name(). However, in C++ and other static typed languages, this is rarely useful; outside of inheritance and generic programming, any variable can only be a single type, and can't change type.

Note that you have to have #include <typeinfo> for typeid to be defined.