Es scheint eine Übereinstimmung zu geben, dass Sie nicht willy nilly (ein int *) in ein char
Array wegen der C++ Aliasing-Regeln zeigen.Ist die Platzierung neu gesetzlich erforderlich, um ein int in ein Char-Array zu setzen?
Von dieser anderen Frage - Generic char[] based storage and avoiding strict-aliasing related UB - scheint es, dass es möglich ist, Speicher durch Placement neu zu verwenden (wieder).
alignas(int) char buf[sizeof(int)];
void f() {
// turn the memory into an int: (??) from the POV of the abstract machine!
::new (buf) int; // is this strictly required? (aside: it's obviously a no-op)
// access storage:
*((int*)buf) = 42; // for this discussion, just assume the cast itself yields the correct pointer value
}
So wird die obige Rechts C++ und wird die Platzierung neuer tatsächlich benötigt es legal zu machen?
In Verbindung stehend: http://stackoverflow.com/questions/38862092/is-it-legal-to-alias-a-char-array-through-a-pointer-to-int –
https://godbolt.org/g/k2nVI9 –
Hoch relevant, potentiell betrogen: https://stackoverflow.com/questions/40873520/reinterpret-cast-creating-a-trivial-default-constructible-object –