[英]init boost::optional of non-copyable object
What should I do to initialize boost::optional< T >
if underlying type T
is non-default constructible, non-copyable/moveable, but one's instance still can exist? 如果底层类型
T
是非默认的可构造的,不可复制/可移动的,但是我的实例仍然可以存在,我该怎么做才能初始化boost::optional< T >
Is it forbidden for boost::optional
by any semantic reasons to have some member function like template< typename... Args > boost::optional< T >::construct(Args && ...args)
, that delivers all the arguments to in-place operator new
to construct the object entirely (for non-ref type T
)? 是否禁止
boost::optional
由于任何语义原因而拥有一些成员函数,如template< typename... Args > boost::optional< T >::construct(Args && ...args)
,它提供所有参数就地operator new
来完全构造对象(对于非ref类型T
)? Variant is to have non-member function like std::make_shared< T >
. Variant具有非成员函数,如
std::make_shared< T >
。
It seems to me, that my problem can be solved by means of using of std::unique_ptr
/ std::shared_ptr
, but in this case my question is: "Why boost::optional
progress is frozen?". 在我看来,我的问题可以通过使用
std::unique_ptr
/ std::shared_ptr
来解决,但在这种情况下我的问题是:“为什么boost::optional
进度被冻结?”。
boost::optional
can be initialized with a non-copyable type by using in-place factories . 可以使用就地工厂使用不可复制的类型初始化
boost::optional
。
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. 在C ++ 14中,提出了一个
std::make_optional
,它可以更好地解决这个问题。 However, this has not been implemented in Boost.Optional. 但是,这还没有在Boost.Optional中实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.