Recently, I heard from my colleagues that a company asked C ++ allocator related questions during an interview.
Allocator templates are essentially a set of memory management requirements of stl containers. They are called concept in generic programming. When you create allocator by yourself, there is no problem as long as you meet these requirements. It is worth noting that a very interesting structure in allocator has to be mentioned. This is rebind. Its purpose is to get another specific allocator class through a specific allocator class, this is the programming of class and can be considered as a simple template meta programming. For example, allocator <int >:: rebind <long >:: other is a allocator <long>.
Rebind mainly serves stl chain containers, such as list. When the list template is embodied as a class, you can specify the element type of the list and the memory allocation policy of the elements (such as list <int, myallocator <int> ), however, the memory allocation policy of the list node cannot be specified. In fact, the memory allocation policy of the list node should be the same as that of the element. In this case, rebind is required to generate allocator for the list node.
In the end, the current allocator can only be used for memory allocation management, but cannot be used for address model adaptation. I don't know if it will change in the future.