c++ - 为什么我必须为分配器的一个实例分配一个值?
问题描述
我从这个链接中得到了关于数组分配器的代码,我在下面显示了关键代码,对于这个问题,我已经删除了构造函数部分,但是我真的不知道为什么我必须ra
用*this
??分配值 如果我只是写ra()
,那将是错误的!
在我看来,因为类array_allocator
继承自std::allocator<T>
,而 的复制构造函数std::allocator
什么都不做。
什么是类型ra
?我注意到c++17和c++2a中的类型不同,因为在c++2a中“ rebind
”结构已经被删除了,所以在之前的版本中类型是std::allocator<int>
,但是在c++ 2a中,类型是array_allocator<int>
. 作为演示节目。那么这会影响结果吗?我很困惑,对不起我的混乱逻辑。欢迎任何答案:)
#include <memory>
#include <type_traits>
template <typename T>
struct array_allocator : std::allocator<T>
{
template <typename C, typename ...Args>
typename std::enable_if<std::is_array<C>::value>::type destroy(C * p)
{
using U = typename std::remove_extent<C>::type;
using UAT = typename std::allocator_traits<array_allocator>::template rebind_traits<U>;
typename std::allocator_traits<array_allocator>::template rebind_alloc<U> ra(*this); ???
for (std::size_t i = 0, e = std::extent<C>::value; i != e; ++i)
{
UAT::destroy(ra, std::addressof((*p)[e - i - 1]));
}
}
template <typename C, typename ...Args>
typename std::enable_if<!std::is_array<C>::value>::type destroy(C * p)
{
p->~C();
}
};
解决方案
推荐阅读
- javascript - C# 对象 [textBox] 不支持 'datepicker' 属性或方法
- python - Django:内置用户与多对多
- r - 为 R 中的某些列条目创建新列
- angular - 加载资源失败:服务器在 ion-calendar 中响应状态为 404(未找到)
- c# - 如何在 RhinoMocks 中存根一个接受 IEnumerable 的方法?
- r - 使用 R 中的 data.table 的虚拟代码折叠列
- nginx - Kubernetes 内部网络监控,没有任何 Sidecar
- visual-studio-2019 - Visual Studio 2019:循环槽嵌套文件
- sockets - 通信后关闭套接字连接
- vba - 在 MS ACCES / VBA 中将参数传递给函数的方法