为什么typeid返回int和const int是相同的类型

cod*_*ddy 13 c++ typeid

if(typeid(int) == typeid(const int))
       cout << "Same types"<< endl;
Run Code Online (Sandbox Code Playgroud)

计划产出:

相同的类型

我错过了什么吗?这些是不同类型大声笑.

Ben*_*igt 14

它们不是同一类型,但typeid操作员剥离constvolatile.

从5.2.8节[expr.typeid]:

glvalue表达式的顶级cv限定符或作为操作数的type-idtypeid始终被忽略.


fre*_*low 6

你可能想要这个:

#include <type_traits>

if (std::is_same<int, const int>::value)
    std::cout << "same types\n";
else
    std::cout << "different types\n";
Run Code Online (Sandbox Code Playgroud)