FAQ Database Discussion Community


reinterpret_cast / static_cast and undefined behavior

c++,c++11,variant,reinterpret-cast,static-cast
In a variant class I'm working on the raw storage is a char array: alignas(/* the strictest alignment of all types of the variant */) char storage[/* ... */]; The assignment operator is something like: template<class X> void operator=(const X &x) { // ...code for clearing the storage and setting...

Using a pointer for casting char* to unsigned char*

c++,pointers,fstream,reinterpret-cast
I'm writing some code that uses the fstream read() function and this function expects a char* as the buffer. Later on, I want to work with the bytes in this buffer as unsigned chars, so I'm either going to have to: 1. declare the buffer as a char* and then...

reinterpret_cast swaps bits?

c++,reinterpret-cast
I was testing a simple compiler when I noticed that its output was completely wrong. In fact, the output had its endianness swapped from little to big. Upon closer examination, the offending code turned out to be this: const char *bp = reinterpret_cast<const char*>(&command._instruction); for (int i = 0; i...

reinterpret_cast from object to first member

c++,c++11,type-conversion,undefined-behavior,reinterpret-cast
I was looking at this answer and I was wondering if casting an object to its first member with reinterpret_cast and using the result could be safe in C++. Let's assume that we have a class A, a class B and an instance b of B: class A{ public: int...

Does reinterpret_casting an integral to a pointer type and back yield the same value?

c++,pointers,language-lawyer,integral,reinterpret-cast
According to http://en.cppreference.com/w/cpp/language/reinterpret_cast, it is known that reinterpret_cast a pointer to an integral of sufficient size and back yield the same value. I'm wondering whether the converse is also true by the standards. That is, does reinterpret_cast an integral to a pointer type of sufficient size and back yield the...

C++ - Safety of reinterpret_cast for pointer-to-primitives

c++,casting,type-safety,reinterpret-cast
I've come across a piece of code in a project I'm evaluating, and wanted to know if the following cast is safe: void func(unsigned long* input); ... int data = 42; func(reinterpret_cast<unsigned long*>(&data)); // Casting to remove compiler warnings about data type not matching type expected I know that with...

Simulating covariance and contravariance in templates with polymorphic parameters using reinterpret_cast and static_cast?

c++,templates,polymorphism,c++14,reinterpret-cast
Let's say I have a simple polymorphic inheritance: struct Base { virtual void exec() { std::cout << "base" << std::endl; } virtual ~Base() { } }; struct Derived1 : public Base { void exec() override { std::cout << "derived1" <<std::endl; } }; struct Derived2 : public Base { void exec()...

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

c++,casting,binary,copy,reinterpret-cast
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...

Is it possible to pass a non-enum value as an enum function parameter?

c++,unit-testing,enums,reinterpret-cast
I have a function that looks like this: std::string SomeClass::convertStateToString(StateE state) const { std::string stateAsString; switch (state) { case UNKNOWN: stateAsString = "UNKNOWN"; break; case OFFLINE: stateAsString = "OFFLINE"; break; case ENABLED: stateAsString = "ENABLED"; break; case DISABLED: stateAsString = "DISABLED"; break; default: stateAsString = "ABNORMAL"; break; } return stateAsString;...

Using `reinterpret_cast` on an enum class - valid or undefined behavior?

c++,language-lawyer,c++14,reinterpret-cast,enum-class
#include <iostream> #include <cassert> #include <type_traits> template<typename T> using Underlying = std::underlying_type_t<T>; enum class ETest : int { Zero = 0, One = 1, Two = 2 }; template<typename T> auto& castEnum(T& mX) noexcept { // `static_cast` does not compile // return static_cast<Underlying<T>&>(mX); return reinterpret_cast<Underlying<T>&>(mX); } int main() { auto...

Once again: strict aliasing rule and char*

c++,c,reinterpret-cast,strict-aliasing
The more I read, the more confused I get. The last question from the related ones is closest to my question, but I got confused with all words about object lifetime and especially - is it OK to only read or not. To get straight to the point. Correct me...