This is the usual way to read object bytes with the ability to three-dimensional copy
Object obj; auto p = reinterpret_cast<char*>(&obj); for(size_t i = 0; i < sizeof(obj); i++) consume(p[i]);
The problem is not with strict anti-aliasing; char* resolves an alias to something. The problem is this excerpt from [expr.add]
When an expression that has an integral type is added or subtracted from the pointer, the result is the type of the operand of the pointer. If the expression P points to the element x[i] object of the array x with elements n , the expressions P + J and J + P (where J has the value J ) indicate the (possibly hypothetical) element x[i + j] if 0 β€ i + j β€ n ; otherwise, the behavior is undefined. Similarly, the expression P - J indicates a (possibly hypothetical) element x[i β j] if 0 β€ i β j β€ n ; otherwise, the behavior is undefined.
Where the hypothetical element refers to
A pointer preceding the last element of an array x elements n is considered equivalent to a pointer to a hypothetical element x[n] for this purpose
In other words, this is legal if arithmetic is on a pointer pointing to an array and the result is still within its range.
However, there is clearly no char[sizeof(Object)] , can we do arithmetic on this pointer?
Note that the legal solution for reading bytes of an object is a std::memcpy object. But if this is the only solution, he asks to ask, why allow char* smoothing if you canβt do anything about it?
c ++ language-lawyer
Passer by
source share