Typical Buffer Interpretations

I have a generic type Typethat can be anything.

Is the following penalty for undefined behavior?

This structure

struct alignas(alignof(Type)) TypeBuffer {
    char buffer[sizeof(Type)];
}

And then freely reinterpret_castbetween Typeand TypeBuffer? Or usually use TypeBufferhow Type? Of course, when I dropped from TypeBufferto Typeand used it, I assume that this part of the memory was built with the placement of a new one or somehow.

If it is undefined, what are the reasons and what are my alternatives to look like a buffer type?

+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1657900/