How to create a container of noncopyable elements

梦想与她 提交于 2019-12-01 17:08:49

No, non-copyable elements can't be in C++ container classes.

According to the standard, 23.1 paragraph 3, "The type of objects stored in these components must met the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types."

One option is to create a list of pointers to the elements (preferrably a shared_ptr). This is not exactly what you want but it will get the job done.

Another option is to use the Boost Pointer Container library. This acts much like a standard container of std::auto_ptrs would, were such a thing possible: it retains exclusive ownership of its elements, and it cannot be copied. It also has less overhead than a standard container of shared_ptrs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!