繁体   English   中英

如何创建C ++ 11不可默认构造的分配器?

[英]How to create a C++ 11 non-default-constructible allocator?

该主题出现在该线程中,涉及到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];

我正在尝试使用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);  
}  

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

typedef unsigned long long uint64_t;
    std::list <uint64_t, Mallocator<uint64_t>> dll; // doubly linked list

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

    std::list<uint64_t, Mallocator<uint64_t>> dll(Mallocator<uint64_t>(0));

我还在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);
}

在主要

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);

但这对于新旧版本的std :: list :: sort都适用,因此它具有默认的构造函数。

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

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

如果您默认构造一个std::list ,它将默认构造其分配器,因此此变量定义仍需要一个默认构造函数:

std::list <uint64_t, Mallocator<uint64_t>> dll; // doubly linked list

如果要在没有默认构造函数的情况下测试分配器,则需要进行其他操作,例如

std::list <uint64_t, Mallocator<uint64_t>> dll(Mallocator<uint64_t>(args));

要么:

Mallocator<uint64_t> alloc(some, args, for, your, allocator);
std::list <uint64_t, Mallocator<uint64_t>> dll(alloc);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM