简体   繁体   English

init boost ::不可复制对象的可选项

[英]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.

相关问题 提升不可复制的怪异感 - Boost non-copyable weirdness 如何初始化不可移动且不可复制的类成员-boost :: optional - How to initialize non-movable and non-copyable class member - boost::optional 如何将非可复制类的默认构造对象放入boost :: container :: map中? - How can a default-constructed object of a non-copyable class be emplaced into a boost::container::map? 使用boost iostreams过滤器(关闭且不可复制) - Using boost iostreams filters (close and non-copyable) 有没有办法对不可复制的对象使用增强信号和插槽? - Is there a way to use boost signals and slots with non-copyable objects? gmock SetArgReferee:设置一个不可复制不可移动的object - gmock SetArgReferee: Set a non-copyable non-moveable object 对于不可复制的对象,boost :: optional vs std :: optional - boost::optional vs std::optional for non copyable objects 无法向 boost::signals2::signal 添加不可复制的连接 - Can't add a non-copyable connection to boost::signals2::signal 对具有不可复制值的stl容器使用boost序列化时出现编译错误 - Compilation error in using boost serialization for stl containers with non-copyable values 后期不可复制成员初始化 - Late non-copyable member initialization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM