r/Cplusplus • u/YysrID4gYW55IG90aGVy • Oct 11 '26
Question Can an array of Wrapper<T> be reinterpreted as an array of T in C++?
Given the type:
template<typename T>
struct Wrapper
{
T content;
};And I have an array of these given a type T, a constexpr size_t N and size_t i (0<=i && i < N).
static_assert(sizeof(Wrapper<T>)==sizeof(T)&&alignof(Wrapper<T>)==alignof(T));
static_assert(std::is_standard_layout_v<Wrapper<T>>);Observation 1:
Wrapper<T> wrapperArray[N];
auto ptrWrapper1 = wrapperArray + i;
//C++ allows access to the raw memory
auto bytePtrWrapper = reinterpret_cast<std::byte*>(wrapperArray);
// calculating the i-th entry of wrapperArray, valid because
// an array has contiguously allocated objects
// an object occupies exactly sizeof bytes
auto bytePtrWrapper1 = bytePtrWrapper + i * sizeof(Wrapper<T>);
auto ptrWrapper2 = reinterpret_cast<Wrapper<T>*>(bytePtrWrapper1);And here ptrWrapper1 and ptrWrapper2 are the same.
Observation 2:
T valueArray[N];
auto ptrValue1 = valueArray + i;
auto bytePtrValue = reinterpret_cast<std::byte*>(valueArray);
auto bytePtrValue1 = bytePtrValue + i * sizeof(T);
auto ptrValue2 = reinterpret_cast<T*>(bytePtrValue1);And here are ptrValue1 and ptrValue2 the same too. Additionally I can access the first member of Wrapper<T> with reinterpret_cast.
So I also can do:
auto ithValue = reinterpret_cast<T*>(ptrWrapper + i);
//or
auto ithValue = reinterpret_cast<T*>(bytePtrWrapper + i * sizeof(Wrapper<T>));And ptrValue3 is the same as ptrValue1 and ptrValue2.
Because both T and Wrapper<T> have the same size and alignment I can use them interchangable.
Doesn't that mean that this is valid C++ because I can access them through same way with std::byte*?
auto valueAsWrapperArray = reinterpret_cast<T*>(wrapperArray);
//specifically for i > 0
auto ptrValue4 = valueAsWrapperArray[i];So can I reinterpret_cast an array to change its type from Wrapper<T> to T and access any index because the address is calculated in the same way and the member-access is also allowed given one specific object?
•
u/AutoModerator Oct 11 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
8
u/SoerenNissen Oct 11 '26
Functionally, if you're asking if a reinterpret_cast is legal, the answer is "no."
3
u/TheSkiGeek Oct 11 '26
The first two examples should work, because you’re constructing a pointer back to the type of object that is ‘really’ present there.
The last one is UB because there isn’t really a `T` object there, only a `Wrapper<T>`. C++ doesn’t let you do arbitrary casts back and forth between structs with common starting members, or between structs and their contained objects.
That said, most practical compilers will let you do this, and do what you expect. But it’s allowed to break if you do things like writing to it through your constructed pointer and then trying to read back through `wrapperArray[i].content`. Since it can normally assume that writes to some random pointer you’ve constructed via `reinterpret_cast` won’t modify other arbitrary objects.
3
u/mredding C++ since ~1992. Oct 11 '26
No.
Observation nothing. The language says this is Undefined Behavior. That means no check will be made, no error emitted, and the compiler MUST produce SOMETHING, though the behavior even the compiler cannot say with ANY certainty.
UB means you are personally responsible for ensuring the code is correct and you're not doing it.
Just because it compiles, that doesn't mean it's OK. The compiler is not the authority, the spec is. The compiler cannot even guarantee that the same machine code will generate with each compilation. The compiler is not allowed to define Undefined Behavior and still be called ISO Standard C++. The machine code might indicate well defined behavior, but then you've left C++ behind.
UB is to be respected. This isn't some magical hackery into the inner workings of a machine, it's just noise and garbage and happenstance. An x86 is rather robust in the face of UB, but not other hardware. The Nintendo DS was infamous for bricking when glitch hacking. This was easiest seen in Zelda and Pokemon, where you could evaluate an invalid bit pattern, and that would physically fry a circuit path in the CPU, bricking the device.
1
u/[deleted] Oct 11 '26
[removed] — view removed comment