当前位置: 首页 > news >正文

c++查看运行时类型

c++查看运行时类型方法有三:

visual studio的监视

c++98查看运行时类型

#include <typeinfo>
#include <iostream>
using namespace std;
int main()
{int i = 0;cout << typeid(i).name() << endl;//intcout << (typeid(i) == typeid(int)) << endl;//1return 0;
}

gcc输出typeid::name();类型的缩写,vs2015可以输出明文的类型字符串,但是无法区分const和&。

Boost::TypeIndex库输出运行时类型

#include <iostream>
#include "boost/type_index.hpp"
using namespace std;
template<typename T>
void f(const T& param)
{using boost::typeindex::type_id_with_cvr;cout << "T=" << type_id_with_cvr<T>().pretty_name() << endl;cout << "param=" << type_id_with_cvr<decltype(param)>().pretty_name() << endl;cout << endl;
}
template<typename T>
void f( T& param)
{using boost::typeindex::type_id_with_cvr;cout << "T=" << type_id_with_cvr<T>().pretty_name() << endl;cout << "param=" << type_id_with_cvr<decltype(param)>().pretty_name() << endl;cout << endl;
}template<typename T>
void f(T&& param)
{using boost::typeindex::type_id_with_cvr;cout << "T=" << type_id_with_cvr<T>().pretty_name() << endl;cout << "param=" << type_id_with_cvr<decltype(param)>().pretty_name() << endl;cout << endl;
}
int main()
{int i = 1;const int& j = i;int* k = &i;f(i);f(j);f(k);f(1);return 0;
}

问题,那个准确?

新建hello world程序

#include <iostream>
#include "boost/type_index.hpp"
using namespace std;
using boost::typeindex::type_id_with_cvr;;
class X {};
void g(X&& val) {}
void f(X&& val) {g(std::move(val));//g(val);//error C2664: “void g(X &&)”: 无法将参数 1 从“X”转换为“X &&”//visual studio watch:X&&cout <<"c++ RTTI="<< typeid(val).name()<<endl;//class Xcout << "boost=" << type_id_with_cvr<decltype(val)>().pretty_name() << endl;//class X && __ptr64int a = 1;
}
void main()
{X x;auto&& a = std::move(x);//visual studio watch:X&&cout <<"c++ RTTI="<< typeid(a).name()<<endl;//class Xcout << "boost=" << type_id_with_cvr<decltype(a)>().pretty_name() << endl;//class X && __ptr64f(std::move(x));
}

结论:

c++ RTTI不准确,

visual studio watch准确,

Boost::TypeIndex准确。

但是看到的类型和实际使用的类型是不同的。

右值定义‌

右值是指那些无法获取到内存地址的值,一般出现在赋值语句的右边,如字面值常量、表达式结果、临时对象等。它们通常不具名,因此无法直接通过变量名来引用。

为什么函数参数类型为&&会自动变成非常量左值?

这是因为,在函数参数列表中,无论是左值引用还是右值引用,参数名都代表了一个在函数作用域内有效的变量。这个变量在函数内部可以被修改、读取,甚至它的地址也可以被获取(尽管对于右值引用来说,获取地址并不是常见的操作,因为通常右值引用绑定的是临时对象或不可寻址的值)。 

  • 右值引用‌:在函数参数列表中,Type&&表示这是一个右值引用参数,它可以绑定到右值上(包括临时对象、返回值优化产生的对象等)。

  • 在函数内部作为左值处理‌:一旦参数被传递给函数,无论它是左值引用还是右值引用,在函数内部它都被视为一个左值。这意味着你可以像处理任何其他左值一样来处理它(例如,修改它的值、调用它的成员函数等)。


http://www.mrgr.cn/news/57159.html

相关文章:

  • 测试自动化成功关键因素!
  • ProteinMPNN中负对数似然损失函数解读
  • LeetCode53:最大子数组和
  • 模拟退火算法:原理与Python实现
  • YOLOv8-seg训练自己的分割数据集
  • Dockerfile 详解
  • Thread类
  • react优化
  • Napkins:开源 AI 开发工具,实现截图或线框图到网页应用的快速转换
  • konva不透明度,查找,显示,隐藏
  • vTESTstudio系列14--vTESTstudio中自定义函数介绍1
  • RHCE时间服务器
  • Vscode + EIDE +CortexDebug 调试Stm32(记录)
  • Kamailio 网络拓扑案例分享
  • C++ set和map的模拟实现
  • Llama Tutor:开源 AI 个性化学习平台,根据主题自动制定学习计划
  • RTDETR 引入 MogaBlock | 多阶门控聚合网络 | ICLR 2024
  • ThinkPad中键打开网页关闭网页失灵
  • 【Linux】线程互斥与同步,生产消费模型(超详解)
  • Redis-05 Redis发布订阅
  • 得物App3D博物馆亮相“两博会”,正品保障助力消费体验升级
  • 10.23Python_matplotlib_乱码问题
  • 三菱FX5U PLC程序容量设置
  • vue3-06-html2canvas使用 + zoom、transform: scale图片缩放适配方案 + 动态引入静态资源(打包上线后也能使用)
  • Java面试题九
  • C语言_动态内存管理