簡體   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