reinterpret-cast

Make interchangeable class types via pointer casting only, without having to allocate any new objects?

倖福魔咒の 提交于 2019-11-27 18:48:24
问题 UPDATE : I do appreciate "don't want that, want this instead" suggestions. They are useful, especially when provided in context of the motivating scenario. Still...regardless of goodness/badness, I've become curious to find a hard-and-fast "yes that can be done legally in C++11" vs "no it is not possible to do something like that" . I want to "alias" an object pointer as another type, for the sole purpose of adding some helper methods. The alias cannot add data members to the underlying class

Why doesn't this reinterpret_cast compile?

﹥>﹥吖頭↗ 提交于 2019-11-27 18:16:15
I understand that reinterpret_cast is dangerous, I'm just doing this to test it. I have the following code: int x = 0; double y = reinterpret_cast<double>(x); When I try to compile the program, it gives me an error saying invalid cast from type 'float' to type 'double What's going on? I thought reinterpret_cast was the rogue cast that you could use to convert apples to submarines, why won't this simple cast compile? By assigning y to the value returned by the cast you're not really casting the value x , you're converting it. That is, y doesn't point to x and pretend that it points to a float.

C++ When should we prefer to use a two chained static_cast over reinterpret_cast

别等时光非礼了梦想. 提交于 2019-11-27 16:06:31
First of all, this is not a duplicate of Why do we have reinterpret_cast in C++ when two chained static_cast can do it's job? . I know situations where we cannot use even two chained static_cast to achieve that, what reinterpret_cast does. But is there any situation where I should prefer a two chained static_cast over a simple and more readable reinterpret_cast ? reinterpret_cast should be a huge flashing symbol that says THIS LOOKS CRAZY BUT I KNOW WHAT I'M DOING. Don't use it just out of laziness. reinterpret_cast means "treat these bits as ..." Chained static casts are not the same because

Is casting std::pair<T1, T2> const& to std::pair<T1 const, T2> const& safe?

ぐ巨炮叔叔 提交于 2019-11-27 14:03:35
Is it safe (in theory or in practice) to reinterpret_cast a std::pair<T1, T2> const & into a std::pair<T1 const, T2> const & , assuming that the programmer hasn't intentionally done something weird like specializing std::pair<T1 const, T2> ? It's NOT portable to do so. std::pair requirements are laid out in clause 20.3. Clause 17.5.2.3 clarifies that Clauses 18 through 30 and Annex D do not specify the representation of classes, and intentionally omit specification of class members. An implementation may define static or non-static class members, or both, as needed to implement the semantics

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

时光毁灭记忆、已成空白 提交于 2019-11-27 08:54:16
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? 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 (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be

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

纵然是瞬间 提交于 2019-11-27 06:53:07
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! So why do we have reinterpret_cast in C++ when two chained static_cast can do its job? Some of you

Why Doesn't reinterpret_cast Force copy_n for Casts between Same-Sized Types?

点点圈 提交于 2019-11-27 05:37:55
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 the (possibly cv-qualified) dynamic type of the object T2 and T1 are both (possibly multi-level,

Is casting std::pair<T1, T2> const& to std::pair<T1 const, T2> const& safe?

你。 提交于 2019-11-27 04:04:58
问题 Is it safe (in theory or in practice) to reinterpret_cast a std::pair<T1, T2> const & into a std::pair<T1 const, T2> const & , assuming that the programmer hasn't intentionally done something weird like specializing std::pair<T1 const, T2> ? 回答1: It's NOT portable to do so. std::pair requirements are laid out in clause 20.3. Clause 17.5.2.3 clarifies that Clauses 18 through 30 and Annex D do not specify the representation of classes, and intentionally omit specification of class members. An

Is C++ considered weakly typed? Why?

混江龙づ霸主 提交于 2019-11-27 03:14:54
问题 I've always considered C++ to be one of the most strongly typed languages out there. So I was quite shocked to see Table 3 of this paper state that C++ is weakly typed. Apparently, C and C++ are considered weakly typed since, due to type-casting, one can interpret a field of a structure that was an integer as a pointer. Is the existence of type casting all that matters? Does the explicit-ness of such casts not matter? More generally, is it really generally accepted that C++ is weakly typed?

Why can't I static_cast between char * and unsigned char *?

风格不统一 提交于 2019-11-27 01:01:04
问题 Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule? 回答1: They are completely different types see standard: 3.9.1 Fundamental types [basic.fundamental] 1 Objects declared as characters char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single