草庐IT

Reinterpret

全部标签

c++ - 在将 void* 转换为任何内容时,我应该使用 static_cast 还是 reinterpret_cast

static_cast和reinterpret_cast似乎都可以很好地将void*转换为另一种指针类型。是否有充分的理由偏爱其中一个? 最佳答案 使用static_cast:它是准确描述此处进行的转换的最窄类型。有一种误解,认为使用reinterpret_cast会更好,因为这意味着“完全忽略类型安全,只是从A转换为B”。但是,这实际上并没有描述reinterpret_cast的效果。相反,reinterpret_cast有多种含义,所有含义都认为“reinterpret_cast执行的映射是实现定义的”。[5.2.10.3]但在

c++ - 在将 void* 转换为任何内容时,我应该使用 static_cast 还是 reinterpret_cast

static_cast和reinterpret_cast似乎都可以很好地将void*转换为另一种指针类型。是否有充分的理由偏爱其中一个? 最佳答案 使用static_cast:它是准确描述此处进行的转换的最窄类型。有一种误解,认为使用reinterpret_cast会更好,因为这意味着“完全忽略类型安全,只是从A转换为B”。但是,这实际上并没有描述reinterpret_cast的效果。相反,reinterpret_cast有多种含义,所有含义都认为“reinterpret_cast执行的映射是实现定义的”。[5.2.10.3]但在

c++ - 当两个链接的 static_cast 可以完成它的工作时,为什么我们在 C++ 中有 reinterpret_cast?

假设我想将A*转换为char*反之亦然,我们有两个选择(我的意思是,我们中的许多人认为我们有两个选择,因为两者似乎都有效!因此困惑!):structA{intage;charname[128];};Aa;char*buffer=static_cast(static_cast(&a));//choice1char*buffer=reinterpret_cast(&a);//choice2两者都可以正常工作。//convertbackA*pA=static_cast(static_cast(buffer));//choice1A*pA=reinterpret_cast(buffer);//

c++ - 当两个链接的 static_cast 可以完成它的工作时,为什么我们在 C++ 中有 reinterpret_cast?

假设我想将A*转换为char*反之亦然,我们有两个选择(我的意思是,我们中的许多人认为我们有两个选择,因为两者似乎都有效!因此困惑!):structA{intage;charname[128];};Aa;char*buffer=static_cast(static_cast(&a));//choice1char*buffer=reinterpret_cast(&a);//choice2两者都可以正常工作。//convertbackA*pA=static_cast(static_cast(buffer));//choice1A*pA=reinterpret_cast(buffer);//

c++ - reinterpret_cast 类型转换成本

我的理解是,C++reinterpret_cast和C指针强制转换只是一个编译时功能,而且它根本没有性能成本。这是真的吗? 最佳答案 这是一个很好的假设。但是,优化器可能会限制在存在reinterpret_cast的情况下它可以假设的内容。或C指针强制转换。然后,即使转换本身没有关联的指令,生成的代码也会变慢。例如,如果您将int转换为指针,优化器可能不知道该指针可能指向什么。因此,它可能不得不假设通过该指针的写入可以更改任何变量。这胜过非常常见的优化,例如将变量存储在寄存器中。 关于c

c++ - reinterpret_cast 类型转换成本

我的理解是,C++reinterpret_cast和C指针强制转换只是一个编译时功能,而且它根本没有性能成本。这是真的吗? 最佳答案 这是一个很好的假设。但是,优化器可能会限制在存在reinterpret_cast的情况下它可以假设的内容。或C指针强制转换。然后,即使转换本身没有关联的指令,生成的代码也会变慢。例如,如果您将int转换为指针,优化器可能不知道该指针可能指向什么。因此,它可能不得不假设通过该指针的写入可以更改任何变量。这胜过非常常见的优化,例如将变量存储在寄存器中。 关于c

c++ - static_cast 和 reinterpret_cast 有什么区别?

这个问题在这里已经有了答案:关闭11年前.PossibleDuplicate:Whenshouldstatic_cast,dynamic_castandreinterpret_castbeused?我在c++中使用c函数,其中在c中作为void类型参数传递的结构直接存储相同的结构类型。例如在C中。voidgetdata(void*data){Testitem*ti=data;//Testitemisofstructtype.}为了在c++中做同样的事情,我使用static_cast:voidfoo::getdata(void*data){Testitem*ti=static_cast(

c++ - static_cast 和 reinterpret_cast 有什么区别?

这个问题在这里已经有了答案:关闭11年前.PossibleDuplicate:Whenshouldstatic_cast,dynamic_castandreinterpret_castbeused?我在c++中使用c函数,其中在c中作为void类型参数传递的结构直接存储相同的结构类型。例如在C中。voidgetdata(void*data){Testitem*ti=data;//Testitemisofstructtype.}为了在c++中做同样的事情,我使用static_cast:voidfoo::getdata(void*data){Testitem*ti=static_cast(