reinterpret-cast

Casting a Struct to an Array [duplicate]

此生再无相见时 提交于 2019-12-02 04:02:02
问题 This question already has answers here : Does accessing the first field of a struct via a C cast violate strict aliasing? (1 answer) Reinterpret struct with members of the same type as an array in a standard compliant way [duplicate] (5 answers) Closed 2 years ago . This is an strict aliasing question, as in will the compiler cause any optimization order problems with this. Say that I have three public float s in a struct XMFLOAT3 (not unlike this one.) And I want to cast to a float* . Will

Casting a Struct to an Array [duplicate]

点点圈 提交于 2019-12-02 00:42:25
This question already has an answer here: Does accessing the first field of a struct via a C cast violate strict aliasing? 1 answer Reinterpret struct with members of the same type as an array in a standard compliant way [duplicate] 5 answers This is an strict aliasing question, as in will the compiler cause any optimization order problems with this. Say that I have three public float s in a struct XMFLOAT3 (not unlike this one .) And I want to cast to a float* . Will this land me in optimization trouble? XMFLOAT3 foo = {1.0f, 2.0f, 3.0f}; auto bar = &foo.x; bar[2] += 5.0f; foo.z += 5.0f; cout

What Type of Cast to Go from Parent to Child?

扶醉桌前 提交于 2019-12-02 00:04:28
问题 This question is about which C++ style cast should be used to make this conversion. I am aware that a C style cast can achieve this. For the following class structure: class Foo {}; class Bar : public Foo {}; Say that I am given: Foo* ptr; and I want to cast it to a Bar* which type of cast should I be using? It seems like I must use dynamic_cast as it is: Used for conversion of polymorphic types I wanted to avoid dynamic_cast since it is a run time cast. 回答1: You are correct that dynamic_cast

What Type of Cast to Go from Parent to Child?

江枫思渺然 提交于 2019-12-01 22:29:49
This question is about which C++ style cast should be used to make this conversion. I am aware that a C style cast can achieve this. For the following class structure: class Foo {}; class Bar : public Foo {}; Say that I am given: Foo* ptr; and I want to cast it to a Bar* which type of cast should I be using? It seems like I must use dynamic_cast as it is: Used for conversion of polymorphic types I wanted to avoid dynamic_cast since it is a run time cast. You are correct that dynamic_cast is usually the most appropriate for this situation. However, if you know that the pointer is actually

Writing to a signed integer as if it is unsigned in C++

只谈情不闲聊 提交于 2019-12-01 22:02:04
问题 Is reinterpret_cast safe for this, and is it the best way to do this? For example, in the code below, I have a class called ibytestream , which allows the reading of uint16_t s and int16_t s from it. ibytestream::next is a vector<unsigned char>::iterator . inline ibytestream& operator>>(ibytestream& stream, uint16_t& data) { data = 0; data |= *stream.next++; data <<= 8; data |= *stream.next++; return stream; } inline ibytestream& operator>>(ibytestream& stream, int16_t& data) { return stream

Writing to a signed integer as if it is unsigned in C++

ε祈祈猫儿з 提交于 2019-12-01 21:34:46
Is reinterpret_cast safe for this, and is it the best way to do this? For example, in the code below, I have a class called ibytestream , which allows the reading of uint16_t s and int16_t s from it. ibytestream::next is a vector<unsigned char>::iterator . inline ibytestream& operator>>(ibytestream& stream, uint16_t& data) { data = 0; data |= *stream.next++; data <<= 8; data |= *stream.next++; return stream; } inline ibytestream& operator>>(ibytestream& stream, int16_t& data) { return stream >> reinterpret_cast<uint16_t&>(data); } I don't want to duplicate the code for converting the bytes to

reinterpret_cast for almost pod data (is layout-compatibility enough)

柔情痞子 提交于 2019-12-01 16:18:02
I am trying to learn about static_cast and reinterpret_cast . If I am correct the standard (9.2.18) says that reinterpret_cast for pod data is safe: A pointer to a POD-struct object, suitably converted using a reinterpret_cast , points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a POD-struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ] My question is how strictly to interpret this. Is, for example, layout-compatibility

static_cast and reinterpret_cast for std::aligned_storage

血红的双手。 提交于 2019-12-01 15:29:33
could someone please explain the bit of code about casting in http://en.cppreference.com/w/cpp/types/aligned_storage please? can the following code return *static_cast<const T*>(static_cast<const void*>(&data[pos])); be replaced with return *reinterpret_cast<const T*>(&data[pos]); ? Why here two casting are used? Thanks a lot. Hong According to the standard (§ 5.2.10 reinterpret_cast , section 7): A pointer to an object can be explicitly converted to a pointer to a different object type. When a prvalue v of type “pointer to T1 ” is converted to the type “pointer to cv T2 ”, the result is static

reinterpret_cast for almost pod data (is layout-compatibility enough)

可紊 提交于 2019-12-01 15:17:57
问题 I am trying to learn about static_cast and reinterpret_cast . If I am correct the standard (9.2.18) says that reinterpret_cast for pod data is safe: A pointer to a POD-struct object, suitably converted using a reinterpret_cast , points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a POD-struct object, but not at its beginning, as necessary to achieve appropriate alignment.

static_cast and reinterpret_cast for std::aligned_storage

时光怂恿深爱的人放手 提交于 2019-12-01 14:24:49
问题 could someone please explain the bit of code about casting in http://en.cppreference.com/w/cpp/types/aligned_storage please? can the following code return *static_cast<const T*>(static_cast<const void*>(&data[pos])); be replaced with return *reinterpret_cast<const T*>(&data[pos]); ? Why here two casting are used? Thanks a lot. Hong 回答1: According to the standard (§ 5.2.10 reinterpret_cast , section 7): A pointer to an object can be explicitly converted to a pointer to a different object type.