1

看下面的代码,

问题是:如何使用 optional<> 延迟构建不可复制的对象。

我在示例中使用了 boost::optional,尽管我相信它现在也在 std::optional 标准中。

是的,我可以使用 scoped_ptr<>,但是我想在堆栈上分配,而不是在堆上。

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

using namespace boost;

struct HardFoo : noncopyable { };

int main()
{
   optional<HardFoo> ok_1( in_place() ); // OK
   // optional<HardFoo> no_1( HardFoo() );  // won't compile

   optional<HardFoo> delay_construct;
   // delay_construct = HardFoo(); // won't compile
   // delay_construct = optional<HardFoo>( in_place() ); // won't compile
   // delay_construct.swap( optional<HardFoo>( in_place() ) ); // won't compile

   return 0;
}

我正在使用 g++,我认为在这种情况下它是 C++03 还是 C++11 无关紧要,因为它是一个基本的设计问题,而不仅仅是代码中的一个问题。

根据要求,一些错误消息,对于这个,我取消注释:

delay_construct = HardFoo();

$ g++ -g -Wall -o test -I/stuff/boost/ test.cpp
In file included from /stuff/boost/utility.hpp:18:0,
                 from test.cpp:2:
test.cpp: In instantiation of ‘void boost::optional_detail::optional_base<T>::assign_value(boost::optional_detail::optional_base<T>::argument_type, boost::optional_detail::optional_base<T>::is_not_reference_tag) [with T = HardFoo; boost::optional_detail::optional_base<T>::argument_type = const HardFoo&; boost::optional_detail::optional_base<T>::is_not_reference_tag = mpl_::bool_<false>]’:
/stuff/boost/optional/optional.hpp:307:12:   required from ‘void boost::optional_detail::optional_base<T>::assign(boost::optional_detail::optional_base<T>::argument_type) [with T = HardFoo; boost::optional_detail::optional_base<T>::argument_type = const HardFoo&]’
/stuff/boost/optional/optional.hpp:606:9:   required from ‘boost::optional<T>& boost::optional<T>::operator=(boost::optional<T>::argument_type) [with T = HardFoo; boost::optional<T> = boost::optional<HardFoo>; boost::optional<T>::argument_type = const HardFoo&]’
test.cpp:14:30:   required from here
/stuff/boost/noncopyable.hpp:28:26: error: ‘const boost::noncopyable_::noncopyable& boost::noncopyable_::noncopyable::operator=(const boost::noncopyable_::noncopyable&)’ is private
test.cpp:6:8: error: within this context
In file included from /stuff/boost/optional.hpp:15:0,
                 from test.cpp:1:
/stuff/boost/optional/optional.hpp:433:69: note: synthesized method ‘HardFoo& HardFoo::operator=(const HardFoo&)’ first required here 
In file included from /stuff/boost/utility.hpp:18:0,
                 from test.cpp:2:
test.cpp: In instantiation of ‘void boost::optional_detail::optional_base<T>::construct(boost::optional_detail::optional_base<T>::argument_type) [with T = HardFoo; boost::optional_detail::optional_base<T>::argument_type = const HardFoo&]’:
/stuff/boost/optional/optional.hpp:308:12:   required from ‘void boost::optional_detail::optional_base<T>::assign(boost::optional_detail::optional_base<T>::argument_type) [with T = HardFoo; boost::optional_detail::optional_base<T>::argument_type = const HardFoo&]’
/stuff/boost/optional/optional.hpp:606:9:   required from ‘boost::optional<T>& boost::optional<T>::operator=(boost::optional<T>::argument_type) [with T = HardFoo; boost::optional<T> = boost::optional<HardFoo>; boost::optional<T>::argument_type = const HardFoo&]’
test.cpp:14:30:   required from here
/stuff/boost/noncopyable.hpp:27:7: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ is private
test.cpp:6:8: error: within this context
In file included from /stuff/boost/optional.hpp:15:0,
                 from test.cpp:1:
/stuff/boost/optional/optional.hpp:346:8: note: synthesized method ‘HardFoo::HardFoo(const HardFoo&)’ first required here 

有一个答案建议直接使用 in_place ,它适用于:

   optional<HardFoo> ok( in_place() );

但不是这个:

   optional<HardFoo> no( in_place<HardFoo>() ); // BAD

错误消息:

$ g++ -g -Wall -o test -I/stuff/boost/ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:15:44: error: no matching function for call to ‘in_place()’
test.cpp:15:44: note: candidates are:
In file included from /stuff/boost/preprocessor/iteration/detail/iter/forward1.hpp:52:0,
                 from /stuff/boost/utility/in_place_factory.hpp:24,
                 from /stuff/boost/optional/optional.hpp:37,
                 from /stuff/boost/optional.hpp:15,
                 from test.cpp:1:
/stuff/boost/utility/in_place_factory.hpp:73:1: note: template<class A0> boost::in_place_factory1<A0> boost::in_place(const A0&)
/stuff/boost/utility/in_place_factory.hpp:73:1: note:   template argument deduction/substitution failed:
test.cpp:15:44: note:   candidate expects 1 argument, 0 provided
In file included from /stuff/boost/preprocessor/iteration/detail/iter/forward1.hpp:57:0,
                 from /stuff/boost/utility/in_place_factory.hpp:24,
                 from /stuff/boost/optional/optional.hpp:37,
                 from /stuff/boost/optional.hpp:15,
                 from test.cpp:1:
/stuff/boost/utility/in_place_factory.hpp:73:1: note: template<class A0, class A1> boost::in_place_factory2<A0, A1> boost::in_place(const A0&, const A1&)
/stuff/boost/utility/in_place_factory.hpp:73:1: note:   template argument deduction/substitution failed:
test.cpp:15:44: note:   candidate expects 2 arguments, 0 provided
In file included from /stuff/boost/preprocessor/iteration/detail/iter/forward1.hpp:62:0,
                 from /stuff/boost/utility/in_place_factory.hpp:24,
                 from /stuff/boost/optional/optional.hpp:37,
                 from /stuff/boost/optional.hpp:15,
                 from test.cpp:1:
4

2 回答 2

3

您需要将 in_place 直接放入对象中:

delay_construct = boost::in_place<Type>(params);

请注意,似乎 boost 直到 1.35 才支持通过工厂(空工厂)的默认构造。

于 2013-08-16T14:00:13.103 回答
3

在 C++14 中,我们将拥有std::optional,而恰好拥有std::optional::emplace. 所以你可以这样做:

std::optional<HardFoo> delay_construct;
delay_construct.emplace(constructor_arg_1, constructor_arg_2);
于 2013-08-16T14:29:11.460 回答