如何创建 C++ 11 非默认可构造分配器?
How to create a C++ 11 non-default-constructible allocator?
这个主题出现在这个线程中,关于 Visual Studio 2015 年对 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::sort.
如果你默认构造一个 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);
这个主题出现在这个线程中,关于 Visual Studio 2015 年对 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::sort.
如果你默认构造一个 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);