I have a quastion for the C++ professionals and members of the C++ standartization commetee.
As i know C++ standart requires an allocator to be a templated parameter in every STL container.
Besides that one can pass reference to allocatior into every STL container. This raises the following questions:
1. Containers that were templatized with different allocators are considered different types and thus cannot be assigned to each other. Should this be considered a drawback or a feature?
2. It is hard to implement pool/shared memory allocation logic since every instance of any STL container will has its own copy of an allocator.
I am not entirely sure whether the first one should be qualified as a drawback or as a feature. Suppose that instead of the way STL implements allocators currently, custom allocators would be inherited from a common base class and every container would have a pointer to that base class. In this case we could easily assign containers created with different allocators to each other. Hoever, I dont know whether we should consider allocators(or pointers to allocators) part of the container data. That is, if allocators were implemented as pointers to base class, would we copy them when one container is assigned to another? Probably not, since the allocation scheme is something given to container at the initialization time and changing that does not make much sense? If we dont consider allocators part of container data, then it is a drawback that in STL we cannot assign containers to each other if they were templatized with different allocators.
As far as the second item is concerned, if allocators were implemented as pointers to base class, it would be easier to share the complicated allocation schemes (such as pool or shared memory allocation) among containers.
Currently each container gets its own copy of alolocator.
Please i would like to hear you opinion regarding this matter (allocators as template parameters vs. allocators inherited from common base class, pointer to which containers can share).
Thanks in advance,
OM
As i know C++ standart requires an allocator to be a templated parameter in every STL container.
Besides that one can pass reference to allocatior into every STL container. This raises the following questions:
1. Containers that were templatized with different allocators are considered different types and thus cannot be assigned to each other. Should this be considered a drawback or a feature?
2. It is hard to implement pool/shared memory allocation logic since every instance of any STL container will has its own copy of an allocator.
I am not entirely sure whether the first one should be qualified as a drawback or as a feature. Suppose that instead of the way STL implements allocators currently, custom allocators would be inherited from a common base class and every container would have a pointer to that base class. In this case we could easily assign containers created with different allocators to each other. Hoever, I dont know whether we should consider allocators(or pointers to allocators) part of the container data. That is, if allocators were implemented as pointers to base class, would we copy them when one container is assigned to another? Probably not, since the allocation scheme is something given to container at the initialization time and changing that does not make much sense? If we dont consider allocators part of container data, then it is a drawback that in STL we cannot assign containers to each other if they were templatized with different allocators.
As far as the second item is concerned, if allocators were implemented as pointers to base class, it would be easier to share the complicated allocation schemes (such as pool or shared memory allocation) among containers.
Currently each container gets its own copy of alolocator.
Please i would like to hear you opinion regarding this matter (allocators as template parameters vs. allocators inherited from common base class, pointer to which containers can share).
Thanks in advance,
OM
Comment