init boost::optional of non-copyable object

浪尽此生 提交于 2019-12-05 02:04:01
Jephir

boost::optional can be initialized with a non-copyable type by using in-place factories.

Specifically, you can use them like this:

#include <boost/optional.hpp>
#include <boost/utility/in_place_factory.hpp>

class MyType : private boost::noncopyable
{ 
public:
  MyType(T1 const& arg1, T2 const& arg2);
}
...
boost::optional<MyType> m_var;
...
m_var = boost::in_place(arg1, arg2);
...

In C++14 there is a proposed std::make_optional that would be a better solution to this problem. However, this has not been implemented in Boost.Optional.

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