简体   繁体   English

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

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

This subject came up in this thread about a change to std::list::sort() for Visual Studio 2015: 该主题出现在该线程中,涉及到Visual Studio 2015对std :: list :: sort()的更改:

`std::list<>::sort()` - why the sudden switch to top-down strategy? `std :: list <> :: sort()`-为什么突然转向自上而下的策略?

The new version of std::list::sort does not require a default constructible std::list, as it only uses iterators, and doesn't create any local lists, so it doesn't matter if lists can't be default constructed. 新版本的std :: list :: sort不需要默认的可构造std :: list,因为它仅使用迭代器,并且不创建任何本地列表,因此列表是否可以为默认值无关紧要建造。 The prior version uses local lists (note - each instance of a list involves a dynamic allocation of a sentinel node): 先前版本使用本地列表(请注意-列表的每个实例都涉及到前哨节点的动态分配):

typedef list<_Ty, _Alloc> _Myt;
    // ...
   const size_t _MAXBINS = 25;
   _Myt _Templist, _Binlist[_MAXBINS];

I'm trying to create a non-default constructible list, with Visual Studio 2015 version to test how the change to std::list::sort() handles this. 我正在尝试使用Visual Studio 2015版本创建一个非默认的可构造列表,以测试对std :: list :: sort()的更改如何处理此问题。

First I tried the Microsoft C++ 11 minimal allocator example. 首先,我尝试了Microsoft C ++ 11最小分配器示例。 udpate - I had to change one line in order for Jonathan Wakely's answer to work and demonstrate the issue: 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);  
}  

update - with Mallocator changed to have no default constructor, this now results in a compile error: 更新 -将Mallocator更改为没有默认构造函数,这现在导致编译错误:

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

Using the suggested change from Jonathan Wakely works and reproduces the issue where the old std::list::sort gets a compile error due to local lists and shows that the new std::list::sort with no local lists works with no default constructor: 使用Jonathan Wakely提出的建议更改,并重现了旧的std :: list :: sort由于本地列表而导致编译错误的问题,并显示了没有本地列表的新std :: list :: sort没有默认值的情况下工作构造函数:

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

I also tried this method based on a thread here at SO: 我还在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);
}

In main 在主要

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

but this works for both the old and new versions of std::list::sort, so it's getting a default constructor. 但这对于新旧版本的std :: list :: sort都适用,因此它具有默认的构造函数。

So the question was how do I create a non default constructible allocator? 所以问题是如何创建一个非默认的可构造分配器?

Thanks to the demo from Igor Tandetni and answer from Jonathan Wakely, I was able to change the Microsoft example allocator above (noted in the comments) to not have a default constructor, and reproduce the issue related to the old std::list::sort. 感谢Igor Tandetni的演示和Jonathan Wakely的回答,我能够将上面的Microsoft示例分配器(注释中指出)更改为没有默认构造函数,并重现了与旧std :: list ::相关的问题分类。

If you default construct a std::list then it will default-construct its allocator, so this variable definition still requires a default constructor: 如果您默认构造一个std::list ,它将默认构造其分配器,因此此变量定义仍需要一个默认构造函数:

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

If you want to test allocators without default constructors you need to do it differently eg 如果要在没有默认构造函数的情况下测试分配器,则需要进行其他操作,例如

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

Or: 要么:

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.

相关问题 如何使用 C++ 11 样式的非默认构造分配器指定初始大小? - How to specify an initial size using C++ 11 style non-default-constructible allocator? 创建非默认可构造类的虚拟对象 - Create dummy object of non-default-constructible class 如何正确初始化非默认可构造的类成员? - How to properly initialize non-default-constructible class member? 如何初始化不可默认构造的不可复制对象的元组? - How to initialize a tuple of non-default-constructible not-copyable objects? 如何初始化std :: array <T, 2> 其中T是不可复制的,非默认可构造的? - How to initialize an std::array<T, 2> where T is non-copyable and non-default-constructible? 移动具有非默认构造 class 保护的构造函数 - Move constructor with protection for non-default-constructible class 使用非默认可构造类型填充std :: array(无可变参数模板) - Populate std::array with non-default-constructible type (no variadic templates) 使用不可默认构造的映射值添加到 std::map - add to std::map with a non-default-constructible mapped value 初始化非默认可构造元素的 std::array? - Initializing an std::array of non-default-constructible elements? 使用 Boost.Python 导出非默认可构造类 - Exporting non-default-constructible classes with Boost.Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM