Why does std :: list want to call my allocator with no arguments?

Sorry, I can’t insert specific code.
I hope this small sample is enough:

Let's say I have a dispenser like this:

template <class T> class MyAllocator { // ... typedefs MyAllocObject _allocObject; public: MyAllocator() { // _allocObject = new .. } MyAllocator(const MyAllocator& alloc) { _allocObject = alloc.getAllocObject(); } template <class U> MyAllocator(const MyAllocator<U>& alloc) { _allocObject = alloc.getAllocObject(); } MyAllocator(const MyAllocObject& allocObject) { _allocObject = allocObject; } inline pointer allocate(size_type size) { return _allocObject->alloc(size); } // other functions }; 

And used as follows:

 MyAllocObject object; MyAllocator<int> myAlloc(object); std::list<int, MyAllocator<int> > list(myAlloc); 

I experienced that if the default constructor is missing, the code does not compile, so I added it.
But the problem is that I rely on this argument because this is what I use for my custom memory allocations.

What can I do in this case?

+5
source share
1 answer

Prior to C ++ 11, STL implementations were allowed to require distributors to behave somehow stagnant.

“acting as if stateless” means that the STL can count on the following:

 MyAllocator a1; void * p = a1.allocate(77, 0); MyAllocator a2; a2.free(p); 

(IIRC, this simplifies the implementation of some container operations.)

"was allowed to require" means that the STL implementation can support state locks (like yours), but not necessary.


C ++ 11 requires stateful generator support.
I could not, however, find a brief introduction to this (does anyone want to add this?) This thread may give you some links .


If you are tied to a specific compiler that does not support distributed resources with state support, you have some not-so-brilliant options:

  • includes a reference to your state as an argument to the dispenser pattern
  • in your distribution, include a backlink to the appropriate allocator (usually destroys the assignment of a custom allocator for small data)
+2
source

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


All Articles