不带'的容器;t要求其元素为默认值且可复制构造 < >我寻找一个C++容器类,它封装了一个类型的对象数组,这些数组不一定是初始化的,不必是默认可构造的或可复制的。对于没有定义良好的复制语义的RAII对象来说,这将非常有趣。这样一个类似容器的类似乎很容易编写(使用分配器分配未初始化的内存和新的位置)。Boost中是否有我刚才忽略的类似内容?我不是在寻找std::vector(它要求其元素是可复制构造的)或指针容器,而是寻找类似以下内容: #include <cstddef> #include <memory> #include <vector> #include <algorithm> #include <iostream> template< typename T, typename Alloc = std::allocator<T> > class FixedVector { public: typedef typename Alloc::value_type value_type; typedef typename Alloc::pointer pointer; typedef typename Alloc::reference reference; typedef typename Alloc::const_pointer const_pointer; typedef typename Alloc::const_reference const_reference; typedef typename Alloc::size_type size_type; typedef typename Alloc::difference_type difference_type; typedef pointer iterator; typedef const_pointer const_iterator; explicit FixedVector(size_type size, const Alloc& allocator = Alloc()): m_alloc(allocator), m_size(size), m_data(m_alloc.allocate(size)), m_constructed(size) { } FixedVector(const FixedVector& other): m_alloc(other.m_alloc), m_size(other.m_size), m_data(m_alloc.allocate(m_size)), m_constructed(other.m_constructed) { for (size_type i = 0; i != m_size; ++i) { if (m_constructed[i]) m_alloc.construct(m_alloc.address(m_data[i]), other[i]); } } ~FixedVector() { for (size_type i = 0; i != m_size; ++i) { if (m_constructed[i]) m_alloc.destroy(m_alloc.address(m_data[i])); } m_alloc.deallocate(m_data, m_size); } FixedVector& operator=(FixedVector other) { other.swap(*this); return *this; } // operator[] and other unimportant stuff void swap(FixedVector& other) { std::swap(m_alloc, other.m_alloc); std::swap(m_size, other.m_size); std::swap(m_data, other.m_data); std::swap(m_constructed, other.m_constructed); } void construct(size_type index) { new (m_alloc.address(m_data[index])) T(); m_constructed[index] = true; } template<typename U> void construct(size_type index, U& val) { new (m_alloc.address(m_data[index])) T(val); m_constructed[index] = true; } template<typename U> void construct(size_type index, const U& val) { new (m_alloc.address(m_data[index])) T(val); m_constructed[index] = true; } private: Alloc m_alloc; size_type m_size; pointer m_data; std::vector<bool> m_constructed; }; template<typename T, typename Alloc> void swap(FixedVector<T, Alloc>& first, FixedVector<T, Alloc>& second) { first.swap(second); } namespace std { template<typename T, typename Alloc> void swap(FixedVector<T, Alloc>& first, FixedVector<T, Alloc>& second) { first.swap(second); } } class Test { public: explicit Test(int val): m_val(val) { std::cout << "Test::Test(" << val << ')' << std::endl; } ~Test() { std::cout << "Test::~Test() [with m_val = " << m_val << ']' << std::endl; } int val() const { return m_val; } private: int m_val; Test(const Test&); Test& operator=(const Test&); }; template<typename Char, typename Traits> std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const Test& object) { return stream << object.val(); } int main() { typedef FixedVector<Test> FVT; FVT w(10); w.construct(7, 7); w.construct(2, 2); std::cout << "w[2] = " << w[2] << std::endl; } #包括 #包括 #包括 #包括 #包括 模板 类固定向量{ 公众: typedef typename Alloc::value_type value_type; typedef typename Alloc::指针; typedef typename Alloc::reference引用; typedef typename Alloc::常量指针常量指针; typedef typename Alloc::const_reference const_reference; typedef typename Alloc::size\u type size\u type; typedef typename Alloc::difference_type difference_type; typedef指针迭代器; typedef常量指针常量迭代器; 显式FixedVector(大小\类型大小,常量Alloc&allocator=Alloc()): m_alloc(分配器), m_尺寸(尺寸), m_数据(m_分配(大小)), m_构造(大小){} 固定向量(常数固定向量和其他): m_alloc(其他m_alloc), m_尺寸(其他m_尺寸), m_数据(m_分配(m_大小)), m_构造(其他m_构造){ 对于(尺寸类型i=0;i!=m尺寸;++i){ 如果(m_构造[i])m_分配构造(m_分配地址(m_数据[i]),其他[i]); } } ~FixedVector(){ 对于(尺寸类型i=0;i!=m尺寸;++i){ 如果(m_构造[i])m_分配销毁(m_分配地址(m_数据[i]); } m_alloc.deallocate(m_数据,m_大小); } FixedVector和运算符=(FixedVector其他){ 其他。互换(*本); 归还*这个; } //接线员[]和其他不重要的东西 无效交换(固定向量和其他){ 标准::交换(m_alloc,其他.m_alloc); 标准::交换(m_大小,其他.m_大小); std::swap(m_数据,其他.m_数据); 标准::交换(m_构造,其他.m_构造); } 无效构造(大小\类型索引){ 新的(m_分配地址(m_数据[索引])T(); m_构造的[索引]=真; } 模板 无效构造(大小\类型索引,U&val){ 新的(m_分配地址(m_数据[索引])T(val); m_构造的[索引]=真; } 模板 无效构造(大小\类型索引、常数和值){ 新的(m_分配地址(m_数据[索引])T(val); m_构造的[索引]=真; } 私人: Alloc m_Alloc; 尺寸类型m尺寸; 指针m_数据; std::向量m_构造; }; 模板 无效交换(FixedVector和first,FixedVector和second){ 第一,互换(第二); } 名称空间标准{ 模板 无效交换(FixedVector和first,FixedVector和second){ 第一,互换(第二); } } 课堂测试{ 公众: 显式测试(int-val):m_-val(val){ std::cout

不带'的容器;t要求其元素为默认值且可复制构造 < >我寻找一个C++容器类,它封装了一个类型的对象数组,这些数组不一定是初始化的,不必是默认可构造的或可复制的。对于没有定义良好的复制语义的RAII对象来说,这将非常有趣。这样一个类似容器的类似乎很容易编写(使用分配器分配未初始化的内存和新的位置)。Boost中是否有我刚才忽略的类似内容?我不是在寻找std::vector(它要求其元素是可复制构造的)或指针容器,而是寻找类似以下内容: #include <cstddef> #include <memory> #include <vector> #include <algorithm> #include <iostream> template< typename T, typename Alloc = std::allocator<T> > class FixedVector { public: typedef typename Alloc::value_type value_type; typedef typename Alloc::pointer pointer; typedef typename Alloc::reference reference; typedef typename Alloc::const_pointer const_pointer; typedef typename Alloc::const_reference const_reference; typedef typename Alloc::size_type size_type; typedef typename Alloc::difference_type difference_type; typedef pointer iterator; typedef const_pointer const_iterator; explicit FixedVector(size_type size, const Alloc& allocator = Alloc()): m_alloc(allocator), m_size(size), m_data(m_alloc.allocate(size)), m_constructed(size) { } FixedVector(const FixedVector& other): m_alloc(other.m_alloc), m_size(other.m_size), m_data(m_alloc.allocate(m_size)), m_constructed(other.m_constructed) { for (size_type i = 0; i != m_size; ++i) { if (m_constructed[i]) m_alloc.construct(m_alloc.address(m_data[i]), other[i]); } } ~FixedVector() { for (size_type i = 0; i != m_size; ++i) { if (m_constructed[i]) m_alloc.destroy(m_alloc.address(m_data[i])); } m_alloc.deallocate(m_data, m_size); } FixedVector& operator=(FixedVector other) { other.swap(*this); return *this; } // operator[] and other unimportant stuff void swap(FixedVector& other) { std::swap(m_alloc, other.m_alloc); std::swap(m_size, other.m_size); std::swap(m_data, other.m_data); std::swap(m_constructed, other.m_constructed); } void construct(size_type index) { new (m_alloc.address(m_data[index])) T(); m_constructed[index] = true; } template<typename U> void construct(size_type index, U& val) { new (m_alloc.address(m_data[index])) T(val); m_constructed[index] = true; } template<typename U> void construct(size_type index, const U& val) { new (m_alloc.address(m_data[index])) T(val); m_constructed[index] = true; } private: Alloc m_alloc; size_type m_size; pointer m_data; std::vector<bool> m_constructed; }; template<typename T, typename Alloc> void swap(FixedVector<T, Alloc>& first, FixedVector<T, Alloc>& second) { first.swap(second); } namespace std { template<typename T, typename Alloc> void swap(FixedVector<T, Alloc>& first, FixedVector<T, Alloc>& second) { first.swap(second); } } class Test { public: explicit Test(int val): m_val(val) { std::cout << "Test::Test(" << val << ')' << std::endl; } ~Test() { std::cout << "Test::~Test() [with m_val = " << m_val << ']' << std::endl; } int val() const { return m_val; } private: int m_val; Test(const Test&); Test& operator=(const Test&); }; template<typename Char, typename Traits> std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const Test& object) { return stream << object.val(); } int main() { typedef FixedVector<Test> FVT; FVT w(10); w.construct(7, 7); w.construct(2, 2); std::cout << "w[2] = " << w[2] << std::endl; } #包括 #包括 #包括 #包括 #包括 模板 类固定向量{ 公众: typedef typename Alloc::value_type value_type; typedef typename Alloc::指针; typedef typename Alloc::reference引用; typedef typename Alloc::常量指针常量指针; typedef typename Alloc::const_reference const_reference; typedef typename Alloc::size\u type size\u type; typedef typename Alloc::difference_type difference_type; typedef指针迭代器; typedef常量指针常量迭代器; 显式FixedVector(大小\类型大小,常量Alloc&allocator=Alloc()): m_alloc(分配器), m_尺寸(尺寸), m_数据(m_分配(大小)), m_构造(大小){} 固定向量(常数固定向量和其他): m_alloc(其他m_alloc), m_尺寸(其他m_尺寸), m_数据(m_分配(m_大小)), m_构造(其他m_构造){ 对于(尺寸类型i=0;i!=m尺寸;++i){ 如果(m_构造[i])m_分配构造(m_分配地址(m_数据[i]),其他[i]); } } ~FixedVector(){ 对于(尺寸类型i=0;i!=m尺寸;++i){ 如果(m_构造[i])m_分配销毁(m_分配地址(m_数据[i]); } m_alloc.deallocate(m_数据,m_大小); } FixedVector和运算符=(FixedVector其他){ 其他。互换(*本); 归还*这个; } //接线员[]和其他不重要的东西 无效交换(固定向量和其他){ 标准::交换(m_alloc,其他.m_alloc); 标准::交换(m_大小,其他.m_大小); std::swap(m_数据,其他.m_数据); 标准::交换(m_构造,其他.m_构造); } 无效构造(大小\类型索引){ 新的(m_分配地址(m_数据[索引])T(); m_构造的[索引]=真; } 模板 无效构造(大小\类型索引,U&val){ 新的(m_分配地址(m_数据[索引])T(val); m_构造的[索引]=真; } 模板 无效构造(大小\类型索引、常数和值){ 新的(m_分配地址(m_数据[索引])T(val); m_构造的[索引]=真; } 私人: Alloc m_Alloc; 尺寸类型m尺寸; 指针m_数据; std::向量m_构造; }; 模板 无效交换(FixedVector和first,FixedVector和second){ 第一,互换(第二); } 名称空间标准{ 模板 无效交换(FixedVector和first,FixedVector和second){ 第一,互换(第二); } } 课堂测试{ 公众: 显式测试(int-val):m_-val(val){ std::cout,c++,templates,containers,C++,Templates,Containers,在C++0x中,std::vector的元素只要是可移动的,就不必是可复制构造的。在C++0x中,std::vector的元素只要是可移动的,就不必是可复制构造的 这样一个类似容器的类似乎 写起来相当容易(使用 分配程序,用于分配未初始化的 内存和位置(新) 这正是std::vector所做的。要使用placementnew,您必须制作一份副本 void store(const T& value) { new (storage) T(value); //<-- invoke

在C++0x中,
std::vector
的元素只要是可移动的,就不必是可复制构造的。

在C++0x中,
std::vector
的元素只要是可移动的,就不必是可复制构造的

这样一个类似容器的类似乎 写起来相当容易(使用 分配程序,用于分配未初始化的 内存和位置(新)

这正是
std::vector
所做的。要使用placement
new
,您必须制作一份副本

void store(const T& value)
{
    new (storage) T(value); //<-- invokes copy constructor
}
void存储(常量和值)
{
新(存储)T(值)//
这样一个类似容器的类似乎
写起来相当容易(使用
分配程序,用于分配未初始化的
内存和位置(新)

这正是
std::vector
所做的。要使用placement
new
,您必须制作一份副本

void store(const T& value)
{
    new (storage) T(value); //<-- invokes copy constructor
}
void存储(常量和值)
{

新(存储)T(值);//Placement new并不意味着可复制性。我建议的类模板使用Placement new,并且不要求类型可复制。@Philipp:好的,是的。您的意思是容器将根据给定的参数构造一个对象。然后,您将遇到转发任意数量的参数的问题。同样,C++0x将处理一些问题。Y是的,我知道转发问题(这就是为什么我在我的示例中只有空构造函数和一元构造函数),但是有许多Boost库,其中大量参数列表是手动编写的,或者使用预处理器模板(例如Boost.Tuple)自动生成的,因此我不认为这是一个一般性问题。使用最多两个参数的重载将非常有用。实际上,有Boost.InPlaceFactory库可以解决转发问题。似乎只缺少相应的容器类。PlaceNew并不意味着可复制性。我建议的类模板使用placement new,不要求类型可复制。@Philipp:好的,是的。你的意思是容器将从给定的参数构造一个对象。然后你将有一个转发任意数量参数的问题。同样,C++0x将处理一些问题。是的,我知道转发问题(这就是为什么在我的示例中我只有空值和一元值构造函数的原因),但是有很多Boost库,其中大量的参数列表是手动编写的,或者使用预处理器或模板(例如Boost.Tuple)自动生成的,所以我不认为这是一个一般性问题。具有最多两个参数的重载将非常有用。实际上,有Boost.InPlaceFactory库可以解决转发问题。似乎只缺少相应的容器类。谢谢,但是Boost中是否也有不需要C++0x的解决方案?谢谢,buBoost中是否还有一个不需要C++0x的解决方案?请注意,您有一个非常奇怪的
操作符=