如何创建一个C++ 11非默认构造的分配器?

rcg*_*ldr 5 c++ list allocator c++11

这个主题出现在这个主题中,关于对Visual Studio 2015的std :: list :: sort()的更改:

`std :: list <> :: sort()` - 为什么突然切换到自上而下策略?

新版本的std :: list :: sort不需要默认的可构造std :: list,因为它只使用迭代器,并且不创建任何本地列表,因此列表不能是默认值无关紧要建造.先前版本使用本地列表(注意 - 列表的每个实例都涉及动态分配标记节点):

typedef list<_Ty, _Alloc> _Myt;
    // ...
   const size_t _MAXBINS = 25;
   _Myt _Templist, _Binlist[_MAXBINS];
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个非默认的可构造列表,使用Visual Studio 2015版本来测试对std :: list :: sort()的更改如何处理.

首先,我尝试了Microsoft C++ 11最小分配器示例.udpate - 为了Jonathan Wakely的回答,我不得不换一行来证明这个问题:

template <class T>  
struct Mallocator  
{  
    typedef T value_type;  
//  Mallocator() noexcept {}  // replaced this line from the Microsoft example
    Mallocator(T) noexcept {} // no default constructor

    // A converting copy constructor:  
    template<class U> Mallocator(const Mallocator<U>&) noexcept {}  
    template<class U> bool operator==(const Mallocator<U>&) const noexcept  
    {  
        return true;  
    }  
    template<class U> bool operator!=(const Mallocator<U>&) const noexcept  
    {  
        return false;  
    }  
    T* allocate(const size_t n) const;  
    void deallocate(T* const p, size_t) const noexcept;  
};  

template <class T>  
T* Mallocator<T>::allocate(const size_t n) const  
{
    if (n == 0)  
    {  
        return nullptr;  
    }  
    if (n > static_cast<size_t>(-1) / sizeof(T))  
    {  
        throw std::bad_array_new_length();  
    }  
    void* const pv = malloc(n * sizeof(T));  
    if (!pv) { throw std::bad_alloc(); }  
    return static_cast<T*>(pv);  
}  

template<class T>  
void Mallocator<T>::deallocate(T * const p, size_t) const noexcept  
{  
    free(p);  
}  
Run Code Online (Sandbox Code Playgroud)

更新 - 将Mallocator更改为没有默认构造函数,这现在导致编译错误:

typedef unsigned long long uint64_t;
    std::list <uint64_t, Mallocator<uint64_t>> dll; // doubly linked list
Run Code Online (Sandbox Code Playgroud)

使用Jonathan Wakely建议的更改工作并重现旧std :: list :: sort由于本地列表而导致编译错误的问题,并显示没有本地列表的新std :: list :: sort无默认工作构造函数:

    std::list<uint64_t, Mallocator<uint64_t>> dll(Mallocator<uint64_t>(0));
Run Code Online (Sandbox Code Playgroud)

我也尝试了这个基于SO的线程的方法:

struct Allocator {
    void construct(void* p, const void* container) const {};
    void destruct(void* p, const void* container) const {};
};

void* operator new (size_t size, const Allocator& alloc, const void* container)
{
    void* allocated_memory = std::malloc(size);
    if (!allocated_memory) {
        throw std::bad_alloc();
    }

    alloc.construct(allocated_memory, container);
    return allocated_memory;
}

void operator delete(void* p, const Allocator& alloc, const void* container)
{
    alloc.destruct(p, container);
    std::free(p);
}
Run Code Online (Sandbox Code Playgroud)

主要的

typedef unsigned long long uint64_t;
// ...
    Allocator alloc;
    std::list<uint64_t> *dll = new(alloc, NULL)std::list<uint64_t>;
    // ...
    operator delete(dll, alloc, NULL);
Run Code Online (Sandbox Code Playgroud)

但是这适用于std :: list :: sort的旧版本和新版本,因此它获得了默认构造函数.

所以问题是我如何创建一个非默认的可构造分配器?

感谢Igor Tandetni的演示和Jonathan Wakely的回答,我能够将上面的Microsoft示例分配器(在注释中注明)更改为没有默认构造函数,并重现与旧std :: list ::相关的问题分类.

Jon*_*ely 5

如果你默认构造a std::list然后它将默认构造它的分配器,所以这个变量定义仍然需要一个默认的构造函数:

std::list <uint64_t, Mallocator<uint64_t>> dll; // doubly linked list
Run Code Online (Sandbox Code Playgroud)

如果你想在没有默认构造函数的情况下测试分配器,你需要做不同的事情,例如

std::list <uint64_t, Mallocator<uint64_t>> dll(Mallocator<uint64_t>(args));
Run Code Online (Sandbox Code Playgroud)

要么:

Mallocator<uint64_t> alloc(some, args, for, your, allocator);
std::list <uint64_t, Mallocator<uint64_t>> dll(alloc);
Run Code Online (Sandbox Code Playgroud)