I was trying to implement my own small allocator for testing purposes and while designing it I thought that I don’t know how to implement it without violating strict aliasing rule.
In most [open-source from github] C++ allocators, method allocate<T>
is an adapter to memory: it runs to the memory, asks for N
consecutive std::byte
s or unsigned char
s, and then reinterpret_cast
‘s this memory to T
, and after that gives memory back to its calee.
However, how does that not break strict aliasing rule (even though calee must call constructors itself, we cast std::byte*
to T*
). How is it possible to work-around it when implementing simple buffer allocator that’s suitable for most STL containers?
1