Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用C++;11样式的非默认可构造分配器?_C++_List_C++11_Allocator - Fatal编程技术网

C++ 如何使用C++;11样式的非默认可构造分配器?

C++ 如何使用C++;11样式的非默认可构造分配器?,c++,list,c++11,allocator,C++,List,C++11,Allocator,在之前的一篇关于Visual Studio 2015更新的帖子中,这篇文章介绍了如何处理没有默认分配器的列表,这是用于创建这样一个列表的示例之一,它基于一个没有默认分配器的Microsoft示例 我试图弄清楚如何创建一个初始(非零)大小的std::list实例,而不必在创建空列表后重新调整大小 // this part of the code based on Microsoft example template <class T> struct Mallocator {

在之前的一篇关于Visual Studio 2015更新的帖子中,这篇文章介绍了如何处理没有默认分配器的列表,这是用于创建这样一个列表的示例之一,它基于一个没有默认分配器的Microsoft示例

我试图弄清楚如何创建一个初始(非零)大小的
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);
//这部分代码基于Microsoft示例
模板
结构Mallocator
{  
类型定义T值_类型;
Mallocator(T)noexcept{}//默认构造函数不是STL所必需的
//转换副本构造函数:
模板Mallocator(const Mallocator&)noexcept{}
模板布尔运算符==(常量Mallocator&)常量noexcept
{  
返回true;
}  
模板布尔运算符!=(常量Mallocator&)常量noexcept
{  
返回false;
}  
分配(常数大小)常数;
无效解除分配(T*常数p,size_T)常数无例外;
};  
模板
T*Mallocator::分配(常量大小\u T n)常量
{
如果(n==0)
{  
返回空ptr;
}  
如果(n>静态_-cast(-1)/sizeof(T))
{  
抛出std::坏数组\新长度();
}  
void*const pv=malloc(n*sizeof(T));
如果(!pv){throw std::bad_alloc();}
返回静态_-cast(pv);
}  
模板
void Mallocator::释放(T*const p,size\T)const noexcept
{  
自由基(p);
}  
typedef无符号长uint64\u t;
#定义计数(4*1024*1024-1)//要排序的值的数目
int main(int argc,字符**argv)
{
//这句话来自先前的回答
//需要(0)来防止编译器错误,但更改
//(0)到(计数)或其他非零值没有影响,大小==0
std::list ll(Mallocator(0));
//试图避免调整列表大小以获得初始大小。
ll.调整大小(计数,0);

我尝试了更多的变体,这触发了Visual Studio显示各种参数组合,12个变体中的第12个以正确的顺序显示参数:(计数、值、分配器)。注意,在VS 2015的情况下,没有(计数、分配器)选项,需要包含该值。最后一个参数(0)中的值无关紧要,它只需要是正确的类型

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

令人惊讶的是,一方面是带有参数(count,allocator)的构造函数;另一方面,据说VS2015在绝大多数情况下都符合C++14(除了)。在VS2017中,这确实实现了,12的变体11。@Bentoy13-对于VS2015,12的变体11是(count,value)。我有一个多引导系统,XP,XP X64,VS2005和VS2010都有,VS2015赢7,VS2019赢10。对std::list::sort()的更改是用VS2015完成的,这就是我关注该版本的原因。我更新了答案,注意到VS2015没有(计数,分配器)作为一种选择。我相信您的测试,但令人惊讶的是,在发行说明中没有提到STL容器构造函数的更改。我认为您的post+答案很有价值。@Bentoy13-对std::list::sort()所做的更改在VS2015中也没有提到。另一点混乱是最后一个参数,它需要一个值,但只是用来确定值的类型,而值本身并不重要,所以我使用了(0)。最初我认为这个值是一个计数,但它只是用来提供一个类型。@Bentoy13-对于VS 2019,15的第14个变量是(计数,分配器),变量15/15是(计数,值,分配器)。