简体   繁体   English

如何使用 C++ 11 样式的非默认构造分配器指定初始大小?

[英]How to specify an initial size using C++ 11 style non-default-constructible allocator?

In a prior thread about an update to Visual Studio 2015 std::list::sort handling a list with no default allocators, this was one of the examples used to create such a list, based on a Microsoft example for no default allocator.在之前关于更新 Visual Studio 2015 std::list::sort处理没有默认分配器的列表的线程中,这是用于创建此类列表的示例之一,基于 Microsoft 的无默认分配器示例。

I'm trying to figure out how to create an instance of a std::list with an initial (non-zero) size, without having to do a resize after creating an empty list.我试图弄清楚如何创建具有初始(非零)大小的std::list实例,而不必在创建空列表后调整大小。

// this part of the code based on Microsoft example
template <class T>  
struct Mallocator  
{  
    typedef T value_type;  
    Mallocator(T) noexcept {} //default ctor not required by STL  

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

typedef unsigned long long uint64_t;

#define COUNT (4*1024*1024-1)   // number of values to sort

int main(int argc, char**argv)
{
    // this line from a prior answer
    // the (0) is needed to prevent compiler error, but changing the
    // (0) to (COUNT) or other non-zero value has no effect, the size == 0
    std::list <uint64_t, Mallocator<uint64_t>> ll(Mallocator<uint64_t>(0));
    // trying to avoid having to resize the list to get an initial size.
    ll.resize(COUNT, 0);

I tried some more variations on this which triggered Visual Studio to show various combinations of parameters, and variation 12 of 12 showed the parameters in the correct order: (count, value, allocator).我尝试了更多的变化,这触发了 Visual Studio 显示参数的各种组合,12 个变化中的第 12 个以正确的顺序显示参数:(计数、值、分配器)。 Note in the case of VS 2015, there is no option for (count, allocator), the value needs to be included.请注意,在 VS 2015 的情况下,(count, allocator) 没有选项,需要包含该值。 The value in the last parameter, <uint64_t>(0), doesn't matter, it just needs to be the proper type.最后一个参数 <uint64_t>(0) 中的值无关紧要,它只需要是正确的类型。

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

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

相关问题 如何创建C ++ 11不可默认构造的分配器? - How to create a C++ 11 non-default-constructible allocator? 使用一元调整大小减小非默认可构造元素的容器大小 - Decrease size of container of non-default-constructible elements using unary resize 如何正确初始化非默认可构造的类成员? - 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 创建非默认可构造类的虚拟对象 - Create dummy object of 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM