Search code examples
c++constantstypeid

Why const is not shown in the output by typeid().name(), c++


I am a beginner in c++.

#include <iostream>
#include <typeinfo>

int main()
{
    const int i = 10;
    std::cout << typeid(i).name() << std::endl;
}

The type of i should be const int, but why the results is int on my laptop? I am using windows 10, Visual studio IDE.


Solution

  • The type of i is const int indeed, but the std::type_info object returned from typeid(i) does refer to int; because const is ignored by typeid.

    In all cases, cv-qualifiers are ignored by typeid (that is, typeid(T) == typeid(const T))