reinterpret-cast

casting via void* instead of using reinterpret_cast

浪尽此生 提交于 2019-11-27 00:20:21
I'm reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast : T1 * p1=... void *pv=p1; T2 * p2= static_cast<T2*>(pv); Instead of: T1 * p1=... T2 * p2= reinterpret_cast<T2*>(p1); However, I can't find an explanation why is this better than the direct cast. I would very appreciate if someone can give me an explanation or point me to the answer. Thanks in advance p.s. I know what is reinterpret_cast used for, but I never saw that is used in this way For types for which such cast is permitted (e.g. if T1 is a POD

Equivalent of C++'s reinterpret_cast in C#

不问归期 提交于 2019-11-26 23:16:29
问题 I wonder what's the equivalent of C++'s reinterpret_cast in C#!? Here's my sample: class Base { protected int counter = 0; } class Foo : Base { public int Counter { get { return counter; } } } Base b = new Base(); Foo f = b as Foo; // f will be null I've got no objection why f will be null since it should be. But if it was C++ I could have written Foo f = reinterpret_cast<Foo>(b); and get what I wanted. What can I do to achieve the same in C#? PS. I'm assuming that Base and Foo are consistent

Is there a (semantic) difference between the return value of placement new and the casted value of its operand?

。_饼干妹妹 提交于 2019-11-26 22:51:48
Is there a (semantic) difference between the return value of placement new and the casted value of its operand? struct Foo { ... }; char buffer[...]; Foo *a = new(buffer) Foo; Foo *b = reinterpret_cast<Foo *>(buffer); Does a and b differ in some way? EDIT: Based on DaBler's comment, this question tells that there is a difference, if const/reference members used: Placement new and assignment of class with const member So, my little-bit updated question: Does a and b differ in any way, if Foo doesn't have const or reference members? Only a can safely be used to directly access the Foo object

C++ unions vs. reinterpret_cast

我的梦境 提交于 2019-11-26 21:11:45
问题 It appears from other StackOverflow questions and reading §9.5.1 of the ISO/IEC draft C++ standard standard that the use of unions to do a literal reinterpret_cast of data is undefined behavior. Consider the code below. The goal is to take the integer value of 0xffff and literally interpret it as a series of bits in IEEE 754 floating point. (Binary convert shows visually how this is done.) #include <iostream> using namespace std; union unionType { int myInt; float myFloat; }; int main() { int

Does accessing the first field of a struct via a C cast violate strict aliasing?

为君一笑 提交于 2019-11-26 14:22:01
问题 Does this code violate strict aliasing? struct {int x;} a; *(int*)&a = 3 More abstractly, is it legal to cast between different types as long as the primitive read/write operations are type correct? 回答1: First, it is legal to cast in C. §6.7.2.1/13: Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member

Why do we have reinterpret_cast in C++ when two chained static_cast can do its job?

大城市里の小女人 提交于 2019-11-26 12:57:55
问题 Say I want to cast A* to char* and vice-versa, we have two choices (I mean, many of us think we\'ve two choices, because both seems to work! Hence the confusion!): struct A { int age; char name[128]; }; A a; char *buffer = static_cast<char*>(static_cast<void*>(&a)); //choice 1 char *buffer = reinterpret_cast<char*>(&a); //choice 2 Both work fine. //convert back A *pA = static_cast<A*>(static_cast<void*>(buffer)); //choice 1 A *pA = reinterpret_cast<A*>(buffer); //choice 2 Even this works fine

Why Doesn&#39;t reinterpret_cast Force copy_n for Casts between Same-Sized Types?

守給你的承諾、 提交于 2019-11-26 12:48:44
问题 According to cppreference.com, reinterpret_cast : Converts between types by reinterpreting the underlying bit pattern. But wait, that\'s a lie cause it only works in these cases: When a pointer or reference to object of type T1 is reinterpret_cast (or C-style cast) to a pointer or reference to object of a different type T2 , the cast always succeeds, but the resulting pointer or reference may only be accessed if both T1 and T2 are standard-layout types and one of the following is true: T2 is

casting via void* instead of using reinterpret_cast

三世轮回 提交于 2019-11-26 09:23:35
问题 I\'m reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast : T1 * p1=... void *pv=p1; T2 * p2= static_cast<T2*>(pv); Instead of: T1 * p1=... T2 * p2= reinterpret_cast<T2*>(p1); However, I can\'t find an explanation why is this better than the direct cast. I would very appreciate if someone can give me an explanation or point me to the answer. Thanks in advance p.s. I know what is reinterpret_cast used for, but

Is there a (semantic) difference between the return value of placement new and the casted value of its operand?

社会主义新天地 提交于 2019-11-26 08:27:46
问题 Is there a (semantic) difference between the return value of placement new and the casted value of its operand? struct Foo { ... }; char buffer[...]; Foo *a = new(buffer) Foo; Foo *b = reinterpret_cast<Foo *>(buffer); Does a and b differ in some way? EDIT: Based on DaBler\'s comment, this question tells that there is a difference, if const/reference members used: Placement new and assignment of class with const member So, my little-bit updated question: Does a and b differ in any way, if Foo

Should I use static_cast or reinterpret_cast when casting a void* to whatever

陌路散爱 提交于 2019-11-26 00:32:42
问题 Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other? 回答1: Use static_cast : it is the narrowest cast that exactly describes what conversion is made here. There’s a misconception that using reinterpret_cast would be a better match because it means “completely ignore type safety and just cast from A to B”. However, this doesn’t actually describe the effect of a reinterpret_cast . Rather,