if(typeid(int) == typeid(const int))
cout << "Same types"<< endl;
程序输出:
Same types
我错过了什么吗? 这些不是相同的类型哈哈。
最佳答案
它们不是同一个类型,但是 typeid
运算符去掉了 const
和 volatile
。
从第 5.2.8 节 [expr.typeid]
:
The top-level cv-qualifiers of the glvalue expression or the type-id that is the operand of
typeid
are always ignored.
关于c++ - 为什么 typeid 返回 int 和 const int 是相同的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888960/