Похоже, что существует некоторая договоренность о том, что вы не можете называть willy nilly (int *) в массив char
из-за правил псевдонимов C++.Является ли размещение новым юридически необходимым для помещения int в массив символов?
Из этого другого вопроса - Generic char[] based storage and avoiding strict-aliasing related UB - кажется, что разрешено (повторно) использовать хранилище путем размещения нового.
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
}
Таким образом, это выше правовой C++ и является размещение нового на самом деле необходимо, чтобы сделать это законно?
Связанный: http://stackoverflow.com/questions/38862092/is-it-legal-to-alias-a-char-array-through-a-pointer-to-int –
https://godbolt.org/g/k2nVI9 –
Очень актуальный, потенциальный обман: https://stackoverflow.com/questions/40873520/reinterpret-cast-creating-a-trivially-default-constructible-object –