14
if(typeid(int) == typeid(const int))
       cout << "Same types"<< endl;

节目输出:

相同类型

我错过了什么吗?这些不是相同的类型,哈哈。

4

2 回答 2

15

它们不是同一类型,但typeid运算符剥离constvolatile.

从第 5.2.8 节开始[expr.typeid]

泛左值表达式的顶级cv 限定符或作为操作数的type-idtypeid始终被忽略。

于 2012-01-17T02:57:25.643 回答
6

你可能想要这个:

#include <type_traits>

if (std::is_same<int, const int>::value)
    std::cout << "same types\n";
else
    std::cout << "different types\n";
于 2012-01-17T06:18:50.740 回答