为什么 typeid 返回 int 和 const int 是相同的类型
why typeid returns that int and const int are same types
if(typeid(int) == typeid(const int))
cout << "Same types"<< endl;
程序输出:
相同类型
我错过了什么吗?这些不是同一类型,哈哈。
它们不是同一类型,但typeid
运算符剥离const
和volatile
。
从第 5.2.8 节[expr.typeid]
:
glvalue 表达式的顶级 cv 限定符或作为
typeid
操作数的类型 id 始终被忽略。
你可能想要这个:
#include <type_traits>
if (std::is_same<int, const int>::value)
std::cout << "same typesn";
else
std::cout << "different typesn";
相关文章:
- 为什么 int 类型的枚举类值不能用作 int
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 比较两个整数在C++中与未知 int 类型的相等性
- 二进制'operator+' 'int*'和'int*'类型的无效操作数
- 使用元编程选择 int 类型,将生成错误
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 复制初始化 - 从 'int' 类型转换为非标量类型
- 初始化cv :: mat ::具有int类型的定义大小的人
- 处理代码重新定义int类型(uint16_t,int16_t等),并不喜欢它
- 为什么没有"int"类型声明时输出不同?
- 在大型程序中将长变量更改为 int 类型
- 无法将 int 类型转换为时间类型(我的类类型)
- C++:无法在按位函数后将 int 类型设置为非标量
- 无法分配给INT类型的成员[10]
- C++ cin 将字符串读取到 int 类型中返回 0
- 为什么 decltype((i)) 是引用类型,而 decltype(i+0) 是 int 类型
- 使用4用于循环的4矩阵上的数组下标的“ int [int]类型”
- std::is_arithmetic 为通用 lambda 中的 int 类型返回 false:未定义的行为?
- 正在施放带有“ Int”类型的临时性