C ++-是否可以创建可选的Promise?

时间:2019-12-11 12:03:27

标签: c++11 promise boost-optional

考虑以下代码:

#include <future>
#include <boost/optional.hpp>

struct S {
  std::promise<int> p;
};

void f() {
  boost::optional<S> o = std::move(S());
}

由于在boost :: optional实现的深处尝试使用S的副本构造函数,因此无法编译。

有什么方法可以克服这个问题并在不复制承诺的情况下兑现承诺?

我试图将移动构造函数显式添加到S

S(S&& s): p(std::move(s.p)) {}
S() = default;

无济于事。

编译器错误消息:

In file included from /usr/include/boost/optional.hpp:15:0,
                 from /tmp/iotpromise.cpp:2:
/usr/include/boost/optional/optional.hpp: In instantiation of ‘void boost::optional_detail::optional_base<T>::construct(boost::optional_detail::optional_base<T>::argument_type) [with T = S; boost::optional_detail::optional_base<T>::argument_type = const S&]’:
/usr/include/boost/optional/optional.hpp:230:20:   required from ‘boost::optional_detail::optional_base<T>::optional_base(boost::optional_detail::optional_base<T>::argument_type) [with T = S; boost::optional_detail::optional_base<T>::argument_type = const S&]’
/usr/include/boost/optional/optional.hpp:526:46:   required from ‘boost::optional<T>::optional(boost::optional<T>::argument_type) [with T = S; boost::optional<T>::argument_type = const S&]’
/tmp/iotpromise.cpp:9:39:   required from here
/usr/include/boost/optional/optional.hpp:346:8: error: use of deleted function ‘S::S(const S&)’
        new (m_storage.address()) internal_type(val) ;
        ^
/tmp/iotpromise.cpp:4:8: note: ‘S::S(const S&)’ is implicitly deleted because the default definition would be ill-formed:
 struct S {
        ^
/tmp/iotpromise.cpp:4:8: error: use of deleted function ‘std::promise<_Res>::promise(const std::promise<_Res>&) [with _Res = int]’
In file included from /tmp/iotpromise.cpp:1:0:
/usr/include/c++/4.9/future:977:7: note: declared here
       promise(const promise&) = delete;
       ^

使用Boost 1.55.0,GCC 6.3

0 个答案:

没有答案