gpt4 book ai didi

c++ - 如何创建 C++ 11 非默认可构造分配器?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:16 25 4
gpt4 key购买 nike

这个主题出现在这个线程中,关于 Visual Studio 2015 的 std::list::sort() 的变化:

`std::list<>::sort()` - why the sudden switch to top-down strategy?

新版本的 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 最小分配器示例。 更新 - 我不得不更改一行以使 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));

我也试过这个方法,基于这里的一个线程:

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::相关的问题列表::排序。

最佳答案

如果您默认构造一个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);

关于c++ - 如何创建 C++ 11 非默认可构造分配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40683637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com