casting via void* instead of using reinterpret_cast [duplicate]

For types for which such cast is permitted (e.g. if T1 is a POD-type and T2 is unsigned char), the approach with static_cast is well-defined by the Standard. On the other hand, reinterpret_cast is entirely implementation-defined – the only guarantee that you get for it is that you can cast a pointer type to any … Read more

Is the std::array bit compatible with the old C array?

This doesn’t directly answer your question, but you should simply use std::copy: T c[N]; std::array<T, N> cpp; // from C to C++ std::copy(std::begin(c), std::end(c), std::begin(cpp)); // from C++ to C std::copy(std::begin(cpp), std::end(cpp), std::begin(c)); If T is a trivially copyable type, this’ll compile down to memcpy. If it’s not, then this’ll do element-wise copy assignment and … Read more

How does qobject_cast work?

This is a little complicated… Remember that qobject_cast<T>(obj) is a way to dynamically cast a QObject to the target type T which also derives from QObject. Now, for this to work, the macro Q_OBJECT should be included in the definition of class T. Apparently, the qt_check_for_QOBJECT_macro call is for checking that the class really contains … Read more

Getting around the reinterpret cast limitation with constexpr

I can’t see how a reinterpret cast in this or similar cases can be any different from arithmetic operators It isn’t portable. You are probably aware of the fact that your code causes undefined behavior, since you dereference a type punned pointer and thus break strict aliasing. Moreover, since C++14, operations that would invoke undefined … Read more

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

There are things that reinterpret_cast can do that no sequence of static_casts can do (all from C++03 5.2.10): A pointer can be explicitly converted to any integral type large enough to hold it. A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer to a function can be … Read more

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

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, reinterpret_cast has a number of … Read more

reinterpret_cast cast cost

It’s a good assumption to start with. However, the optimizer may be restricted in what it can assume in the presence of a reinterpret_cast<> or C pointer cast. Then, even though the cast itself has no associated instructions, the resulting code is slower. For instance, if you cast an int to a pointer, the optimizer … Read more

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

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 … Read more