Reputation: 228
In this link from the isocpp.org faq in the example provided, a Fred object is being constructed with placement new to a buffer that is being allocated for another object i.e. for
char memory[sizeof(Fred)]
As I know the strict aliasing rules allows us to do the opposite i.e. for an object of whatever type, we are allowed to have a char*
point at it and we can dereference that pointer and use it as we want.
But here in the example the opposite is happening. What am I missing?
Upvotes: 2
Views: 231
Reputation: 141648
Placement-new creates a new object. It doesn't alias the old object. The old object (the char
array in this example) is considered to stop existing when the placement-new executes.
Before placement-new, there is storage filled with char
objects. After placement-new, there is storage filled with one Fred
object.
Since there is no aliasing, there are no strict-aliasing problems.
Upvotes: 1
Reputation: 170239
The strict aliasing rules doesn't mention that Fred*
must be cast to char*
. Only that variables of type char*
and Fred*
may point to the same object, and be used to access it.
Quoting [basic.lval] paragraph 8
If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
the dynamic type of the object,
[..]
a char or unsigned char type.
Upvotes: 2