替换接受非标准可构造且不可分配类型的向量
我有一个类 test
,由于某些原因,它不是标准可构造的,也不是可分配的。然而它是可复制构造的——可以说它的行为有点像引用。
不幸的是,我需要这些元素的动态数组,并意识到 vector
不是正确的选择,因为向量的元素必须是标准的可构造和可分配的。 解决了这个问题
- 幸运的是,我通过使用
vector
和::reserve vector
而不是::push_back vector
::resize< /code> 并直接填充条目(无标准构造) 赋值的复制'n'交换技巧以及
向量
通常使用 Pimpl-idiom 实现的事实(无直接赋值)现有的test
元素),即类基{ 私人的: std::vector<测试>向量; /* ... */ 民众: /* ... */ 基地&运算符=(以y为底){ 交换(y); 返回*这个; } 无效交换(基数和y){ 使用 std::swap; 交换(向量,y.vect); } /* ... */ };
现在我假设我可能没有考虑到每一点,最重要的是,所有这些技巧都强烈依赖于实现。该标准仅保证标准可构造和可分配类型的标准行为。
现在下一步是什么?如何获取 test
对象的动态数组?
备注:我必须更喜欢标准 C++ 提供的内置解决方案和类。
编辑:我刚刚意识到我的技巧实际上不起作用。如果我定义了一个真正*不可分配的类,我的编译器上会出现很多错误。因此,问题归结为最后一个问题:如何拥有这些 test
对象的动态数组?
(*) 我的 test
类提供了一个赋值运算符,但这一个的工作方式类似于对引用的赋值。
I have a class test
which isn't standard constructable nor assignable due to certain reasons. However it is copy constructable - on may say it behaves a bit like a reference.
Unfortunately I needed a dynamic array of these elements and realized that vector<test>
isn't the right choice because the elements of a vector must be standard constructable and assignable. Fortunately I got around this problem by
- using
vector<T>::reserve
andvector<T>::push_back
instead ofvector<T>::resize
and direct filling the entries (no standard construction) the copy'n'swap trick for assignment and the fact that a
vector
is usually implemented using the Pimpl-idiom (no direct assignment of an existingtest
element), i.eclass base { private: std::vector<test> vect; /* ... */ public: /* ... */ base& operator= (base y) { swap(y); return *this; } void swap(base& y) { using std::swap; swap(vect, y.vect); } /* ... */ };
Now I assume that I probably didn't considered every tiny bit and above all these tricks are strongly implementation dependent. The standard only guarantees standard behavior for standard constructable and assignable types.
Now what's next? How can I get a dynamic array of test
objects?
Remark: I must prefer built in solutions and classes provided by the standard C++.
Edit: I just realized that my tricks actually didn't work. If I define a really* non assignable class I get plenty of errors on my compiler. So the question condenses to the last question: How can I have a dynamic array of these test
objects?
(*) My test
class provided an assignment operator but this one worked like the assignment to a reference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:下面的内容不再是好的做法。如果您的对象支持移动,那么它可能会适合向量(请参阅
std ::vector
元素要求 了解详细信息,特别是 C++17 的更改)。考虑使用 Boost 的 ptr_vector ,Boost Pointer Container Library 的一部分。请特别参阅该库的 动机。
Edit: The below is no longer good practice. If your object supports moving then it will probably fit into a vector (see the
std::vector
elements requirements for details, in particular the changes for C++17).Consider using Boost's ptr_vector, part of the Boost Pointer Container Library. See in particular advantage #3 in that library's motivation.
使用指针向量怎么样?
How about using a vector of pointers?
编写您自己的动态数组类。听起来比尝试让 STL 与这种奇怪的类型一起工作要少一些工作。
Write your own dynamic array class. Sounds like less work than trying to make the STL one work with that strange type.
您可能想查看 Boost.Intrusive - - 尽管这意味着您需要更改
test
的类型以及在内存中放置test
实例的位置。You might want to look at Boost.Intrusive -- although that would mean you would need to change the type of
test
and where in memory you put the instances oftest
.