How to overwrite default behavior of construct method in allocator class in C ++ STL

How do you rewrite the default behavior of the construct method in the allocator class in STL? The following does not work:

#include <list>
#include <iostream>
#include <memory>

struct MyObj {
    MyObj() {
        std::cout << "This is the constructor" << std::endl;
    }
    MyObj(const MyObj& x) {
        std::cout << "This is the copy constructor" << std::endl;
    }
};

class MyAlloc : public std::allocator <MyObj>{
public:
    void construct(pointer p, const_reference t){
        std::cout << "Construct in the allocator" << std::endl;
        new( (void*)p ) MyObj(t);
    }
};

int main(){
    MyObj x;         
    std::list <MyObj,MyAlloc> list(5,x);
} 

This program returns

This is the constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor

I wish he was back

This is the constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
+2
source share
2 answers

Welcome to the wonderful world of dispensers. I hope you enjoy your stay, although this is unlikely.

Rule number 1: It does not work out std::allocator. If you want to use your own distribution scheme, write your own distributor. If you want to override some functions in std :: allocator, just create an instance std::allocatorand call its functions in un-overridden functions.

, . ++ 03 , v- . . std::allocator .

№2: std::list<T> T. : std::list - . , T . , , node , - , .

rebind, typedef other, . std::list :

MyAlloc::rebind<_ListInternalNodeType>::other theAllocatorIWillActuallyUse();

. , MyAlloc::rebind<_ListInternalNodeType>::other std::allocator<_ListInternalNodeType>. , std::list .

+11

, , . , , , :

template<typename T>
class MyAlloc : public std::allocator <T>
{
public:
     typedef size_t     size_type;
     typedef ptrdiff_t  difference_type;
     typedef T*         pointer;
     typedef const T*   const_pointer;
     typedef T&         reference;
     typedef const T&   const_reference;
     typedef T          value_type;


     template<typename U>
     struct rebind
     {
       typedef MyAlloc <U> other; 
     };

     MyAlloc() {}

     template<typename U>
     MyAlloc(const MyAlloc<U>&) {}

     void construct(pointer p, const_reference t){
        std::cout << "Construct in the allocator" << std::endl;
        new( (void*)p ) MyObj(t);
     }

};

:

   int main(){
    MyObj x;         
    std::list <MyObj,MyAlloc<MyObj> > list(5,x);
}

( ):

This is the constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor

- : http://www.ideone.com/QKdqm

rebind std::allocator, :

template<typename U>
struct rebind
{
    typedef std::allocator<U> other; 
};

:

template<typename U>
struct rebind
{
    typedef MyAlloc<U> other; 
};

rebind<U>::other, .

, typedefs () ( , MyAlloc ). :

template<typename T>
class MyAlloc : public std::allocator <T>
{
    typedef std::allocator <T> base;
public:
     typedef typename base::size_type        size_type;
     typedef typename base::difference_type  difference_type;
     typedef typename base::pointer          pointer;
     typedef typename base::const_pointer    const_pointer;
     typedef typename base::reference        reference;
     typedef typename base::const_reference  const_reference;
     typedef typename base::value_type       value_type;

     //same as before
 };

: http://www.ideone.com/LvQhI

+4

All Articles