jjzjj

reinterpret

全部标签

c++ - 如何在 C++ 中使用 reinterpret_cast?

我知道C++中的reinterpret_cast可以这样使用:floata=0;intb=*reinterpret_cast(&a);但是为什么不能直接施法呢?floata=0;intb=reinterpret_cast(a);error:invalidcastfromtype'float'totype'int' 最佳答案 全部reinterpret_cast确实是允许您以不同的方式读取您传递的内存。你给它一个内存位置,并要求它读取该内存,就好像它是你要求它读的一样。这就是为什么它只能与指针和引用一起使用。我们以这段代码为例:#in

c++ - 'reinterpret_cast' : cannot convert from 'overloaded-function' to 'intptr_t' with boost. dll

我正在使用Boost.dll开发插件系统#include#include#includeclassbase{public:base(){};~base(){};templatestaticstd::shared_ptrcreate(){returnstd::make_shared();}virtualvoiddo1()=0;};classderived:publicbase{public:derived(){};~derived(){};virtualvoiddo1()override{}};BOOST_DLL_ALIAS(base::create,//();当我尝试在BOOST_DL

c++ - 为什么我需要从 `long &` 到 `int &` 的 reinterpret_cast,两者都是 32 位 (Windows LLP64)?

我在跨平台环境中使用Qt。我们遇到了以下问题:在Windows上,int和longint都是32位整数;在64位MacOS和Linux上,int是32位的,longint是64位的(参见https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models)。因此,跨平台库倾向于提供它们自己的固定位类型定义。在Windows上,Qt将quint32定义为unsignedint并且不使用unsignedlong整数。另一个库将其Uint32定义为unsignedlong。因此,两者实际上都是32位无符号整数,但具有不同的原始数据

C++ 是否 reinterpret_cast 总是返回结果?

我有两个类,A和B。A是B的父类,我有一个函数接收指向A类型类的指针,检查它是否也是B类型,如果是将调用另一个函数,该函数接受一个指向类型B的类的指针。当函数调用另一个函数时,我提供reinterpret_cast(a)作为参数。如果这看起来模棱两可,这里有一个代码示例:voidabc(A*a){if(a->IsA("B")){//pleasedontworrymuchaboutthisline,//myrealconcernisthereinterpret_castdef(reinterpret_cast(a));};};现在您知道我是如何调用“def”的了,我想知道reinterp

c++ - constant 和 reinterpret cast 是否在编译时发生?

我读到过static_cast发生在编译时,dynamic_cast发生在运行时,因此比static_cast慢。dynamic_cast可以返回空指针(当与指针一起使用时)或以其他方式抛出错误的转换异常。我的问题是reinterpret_cast和const_cast是发生在编译时还是运行时?我认为解释转换发生在运行时,因为它的行为类似于dynamic_cast指示转换是否成功。我对么?const_cast是编译时间吗? 最佳答案 动态转换是唯一需要在运行时“计算”的。所有其他类型转换均在编译时计算。static_cast的机器代

c++ - reinterpret_casting 一个指针类型的整数和返回是否产生相同的值?

根据http://en.cppreference.com/w/cpp/language/reinterpret_cast,众所周知,reinterpret_cast是一个指向足够大的整数的指针,并且返回相同的值。我想知道反过来是否也符合标准。也就是说,reinterpret_cast是否是具有足够大小的指针类型的整数并返回相同的值? 最佳答案 不,标准不保证这一点。引用C++14(n4140)[expr.reinterpret.cast]中涉及指针-整数转换的所有部分,强调我的:4Apointercanbeexplicitlycon

c++ - reinterpret_cast 右值和优化

我正在将一堆代码转换为使用C++风格的转换(在-Wold-style-cast的帮助下)。我并不完全相信它对原始变量的使用,但总的来说我是C++风格转换的新手。一个问题发生在一些字节序转换代码中。当前代码如下所示:#defineREINTERPRET_VARIABLE(VAR,TYPE)(*((TYPE*)(&VAR)))//...uint16_treverse(uint16_tval){/*stufftoreverseuint16_t*/}int16_treverse(int16_tval){uint16_ttemp=reverse(REINTERPRET_VARIABLE(val,

c++ - 如何在没有 reinterpret_cast 的情况下使用 dlsym() 加载函数?

我正在尝试使用clang-tidy来执行C++核心指南。虽然它确实有很多有效点,但有一件事我无法真正解决:dlsym返回一个void*,我需要以某种方式将其转换为适当的函数指针。为此,我使用了reinterpret_cast。由于准则禁止这样做,因此我对此提出了警告。当然,我可以在任何地方放置//NOLINT注释,但我正在寻找一种不使用reinterpret_cast的解决方案,这样警告就会消失。这个问题有什么解决方法吗? 最佳答案 除了reinterpret_cast之外,该语言没有其他方法可以将函数指针类型转换为对象指针类型。这

c++ - reinterpret_cast unsigned char* as uint64_t* - 这是 UB 吗?

假设我们采用一个非常大的unsignedchar数组。std::arrayblob;//...fillarray...(注意:它已经对齐了,问题不在于对齐。)然后我们将其作为uint64_t[]并尝试访问它:constautoptr=reinterpret_cast(blob.data());std::cout转换为uint64_t然后从中读取对我来说看起来很可疑。但是UBsan,-Wstrict-aliasing并没有触发它。Google在FlatBuffers中使用了这种技术.此外,Cap'n'Proto使用此too.这是未定义的行为吗? 最佳答案

c++ - 将任何数据类型序列化为 vector<uint8_t> - 使用 reinterpret_cast?

我没有在搜索中找到任何直接相关的内容,所以如果这是重复的,请原谅。我要做的是通过网络连接序列化数据。我的方法是将我需要传输的所有内容转换为std::vector并在接收方将数据解压缩到适当的变量中。我的方法是这样的:templateinlinevoidpack(std::vector&dst,T&data){uint8_t*src=static_cast(static_cast(&data));dst.insert(dst.end(),src,src+sizeof(T));}templateinlinevoidunpack(vector&src,intindex,T&data){cop