gpt4 book ai didi

c++ - 如何现代化 VC++17/20 的分配器适配器

转载 作者:搜寻专家 更新时间:2023-10-31 01:29:59 25 4
gpt4 key购买 nike

我有一个分配器适配器,我称之为 ctor_allocator。我写它更多的是为了实验而不是为了任何实际需要。它使分配器在可行时使用默认构造,从而绕过任何通过赋值进行的默认初始化。例如, double vector 的构造不会将值初始化为 0.0(或任何值)。

适配器中的许多代码都是过时的,并且看起来可能很快就会被弃用(如果不是的话)。我还没有成功地使代码现代化而不违背它的目的。例如,如果我删除 struct rebind 模板,零初始化返回。

您能展示如何对其进行现代化改造吗?

答案可能对其他应用有指导意义。

编辑:在评论中,Brandon 将这两个链接作为“新方式”的示例。 SO questionMinimal example .两者都不能阻止用零初始化,这发生在 construct 中。但是,如果我通过添加 void construct(U* ptr) 模板修改任一示例,则可以避免零初始化。这回答了一个与我在这里问的不同的问题,但这是对一个好问题的一个很好的回答。

#include <memory>   
namespace dj {
template <typename T, typename A = std::allocator<T>>
class ctor_allocator : public A
{
using a_t = std::allocator_traits<A>;
public:
using A::A; // Inherit constructors from A
template <typename U>
struct rebind {
using other = ctor_allocator<U, typename a_t::template rebind_alloc<U>>;
};

template <typename U>
void construct(U* ptr)
noexcept(std::is_nothrow_default_constructible<U>::value)
{
::new(static_cast<void*>(ptr)) U;
}

template <typename U, typename...Args>
void construct(U* ptr, Args&&... args) {
a_t::construct(static_cast<A&>(*this),
ptr, std::forward<Args>(args)...);
}
};
}

// Test case. main.cpp
#include <iostream>
#include <vector>

template<class T>
using vector = std::vector<T, dj::ctor_allocator<T>>;

int main() {
{
vector<int> v(10);
for (int i = 0; i < 10; ++i) {
v[i] = i * 56313;
}
}
// If ctor_allocator works as intended,
// this probably will not print all zeros.
vector<int> v(10);
for (int i = 0; i < 20; ++i) {
std::cout << std::hex << v[i] << " ";
}
std::cout << std::endl;
}

最佳答案

感谢 Brandon 的提示,我想通了 - 至少大部分。一个可能的解决方案是将 allocatedeallocate 委托(delegate)给基类,而不是从它继承。至少在 VC++2017 上,我仍然需要专门化 construct,尽管我已经看到文档表明我提供的专门化正是默认的。

以下可能需要一些复制构造函数和赋值运算符的东西。请指教。

#include <memory>

namespace dj {

template<typename T, typename Base = std::allocator<T>>
struct ctor_allocator
{
Base A;
using value_type = typename Base::value_type;
using pointer = typename Base::pointer;

pointer allocate(std::size_t n, const void* hint = 0)
{
auto ret = A.allocate(n,hint);
return ret;
}

void deallocate(pointer ptr, std::size_t n)
{
A.deallocate(ptr, n);
}

template <typename U >
void construct(U* ptr) {
::new (ptr) U;
}
};

template <typename T, typename U>
inline
bool operator==(const ctor_allocator<T>& left, const ctor_allocator<U>& right)
{
return left.A == right.A;
}

template <typename T, typename U>
inline bool operator != (const ctor_allocator<T>& a, const ctor_allocator<U>& b)
{
return !(a == b);
}
}

// MAIN.cpp
#include <vector>
template<class T>
using vector = std::vector<T, dj::ctor_allocator<T>>;

int main() {
{
vector<int> v(10);
for (int i = 0; i < 10; ++i) {
v[i] = i * 313;
}
}
vector<int> v(20);
for (auto i: v) {
std::cout << std::hex << i << " ";
}
std::cout << std::endl;
}

关于c++ - 如何现代化 VC++17/20 的分配器适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816306/

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