C++ 如何在容器类移动构造函数中移动分配器对象
我正在实现一个环形缓冲区容器类:C++ 如何在容器类移动构造函数中移动分配器对象,c++,allocator,move-constructor,C++,Allocator,Move Constructor,我正在实现一个环形缓冲区容器类: template <class T, class A = std::allocator<T>> class ring { private: size_type cap_; // the capacity of the array alloc_type alloc_; // the allocator pointer array_; ... public: ring(size_type n,
template <class T, class A = std::allocator<T>>
class ring {
private:
size_type cap_; // the capacity of the array
alloc_type alloc_; // the allocator
pointer array_;
...
public:
ring(size_type n, const alloc_type &a = alloc_type()) : cap_{n}, alloc_{a}, array_{alloc_.allocate((size_t)cap_)}, ... {
memset(array_, 0, (size_t)cap_ * sizeof(T));
}
...
};
您可以将分配的内存包装在一个
共享\u ptr
中,并创建一个自定义删除器。我不确定您希望得到什么样的答案。“是的,您需要确保分配器传输安全”?标准中没有任何内容说明对象从中移动后的外观(当然,对于标准库类型,它会指定,但仅限于这些类型)。因此,没有具体的答案,我们可以给你的代码显示。对于初学者,我们需要知道您打算对A
(分配器)施加什么限制,以及在“从移动”状态下,您打算在容器上执行什么操作。我希望“从移动”环对象是垃圾,这是我通常从“从移动”对象中期望的。我只是想知道我应该如何处理从分配器移出的内存,而不是别的。您可以将分配的内存包装在共享\u ptr
中,并创建一个自定义删除器。我不确定您希望得到什么样的答案。“是的,您需要确保分配器传输安全”?标准中没有任何内容说明对象从中移动后的外观(当然,对于标准库类型,它会指定,但仅限于这些类型)。因此,没有具体的答案,我们可以给你的代码显示。对于初学者,我们需要知道您打算对A
(分配器)施加什么限制,以及在“从移动”状态下,您打算在容器上执行什么操作。我希望“从移动”环对象是垃圾,这是我通常从“从移动”对象中期望的。我只是想知道我应该如何处理从分配器中移出的内存,而不是别的。
ring(self_type&& other) : cap_{other.cap_}, alloc_{other.alloc_}, ... {
other.alloc_ = ??? // must I do something with the other's allocator as I would with a pointer to heap memory?
}