Integer index-able RAII container for non-copyable type

你。 提交于 2019-12-05 16:26:32

One option would be to upgrade to a C++11-compliant Standard Library implementation.

In C++11, the vector(size_type) constructor default constructs N elements into the container. It neither copies nor moves any elements.

Visual C++ 2010 does not support this C++11 feature; I believe the Visual C++ 11 Developer Preview does correctly support it though. I do not know whether recent versions of libstdc++ support this; I would suspect that libc++ does.

Boost.Container, new in Boost 1.48, has a boost::container::vector which provides this feature. It's C++03 conforming, with select C++11 features.

Well, there is one C++03 that boost::container::vector doesn't conform with: vector<bool> is actually a vector of bools. Though I imagine most people would count that as a benefit.

Call it a workaround, but when I need containers of NoCopy types I usually use boost::ptr_vector or std::vector< shared_ptr >.

Obviously, it's slightly more expensive, but luckily for me that has not been a problem for me yet.

The good thing about boost::ptr_vector is that it does automatic dereferencing on some accesses. Check out the docs.

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