boost - C++ optional<> and delayed construction of noncopyable object -
see below code,
the question is: how can delay construction of object non-copyable, using optional<>.
i'm using boost::optional in example, although believe in std::optional standard too.
yes, use scoped_ptr<>, wanted allocate on stack, not heap.
#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; } i'm using g++, assume wouldn't matter whether c++03 or c++11 in case, fundamental design issue rather stuff in code.
as requested, error messages, one, uncommented:
delay_construct = hardfoo(); $ g++ -g -wall -o test -i/stuff/boost/ test.cpp in file included /stuff/boost/utility.hpp:18:0, 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 ‘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 ‘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 here /stuff/boost/noncopyable.hpp:28:26: error: ‘const boost::noncopyable_::noncopyable& boost::noncopyable_::noncopyable::operator=(const boost::noncopyable_::noncopyable&)’ private test.cpp:6:8: error: within context in file included /stuff/boost/optional.hpp:15:0, test.cpp:1: /stuff/boost/optional/optional.hpp:433:69: note: synthesized method ‘hardfoo& hardfoo::operator=(const hardfoo&)’ first required here in file included /stuff/boost/utility.hpp:18:0, 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 ‘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 ‘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 here /stuff/boost/noncopyable.hpp:27:7: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ private test.cpp:6:8: error: within context in file included /stuff/boost/optional.hpp:15:0, test.cpp:1: /stuff/boost/optional/optional.hpp:346:8: note: synthesized method ‘hardfoo::hardfoo(const hardfoo&)’ first required here there answer suggesting use in_place directly, works this:
optional<hardfoo> ok( in_place() ); but not this:
optional<hardfoo> no( in_place<hardfoo>() ); // bad the error messages:
$ g++ -g -wall -o test -i/stuff/boost/ test.cpp test.cpp: in function ‘int main()’: test.cpp:15:44: error: no matching function call ‘in_place()’ test.cpp:15:44: note: candidates are: in file included /stuff/boost/preprocessor/iteration/detail/iter/forward1.hpp:52:0, /stuff/boost/utility/in_place_factory.hpp:24, /stuff/boost/optional/optional.hpp:37, /stuff/boost/optional.hpp:15, 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 /stuff/boost/preprocessor/iteration/detail/iter/forward1.hpp:57:0, /stuff/boost/utility/in_place_factory.hpp:24, /stuff/boost/optional/optional.hpp:37, /stuff/boost/optional.hpp:15, 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 /stuff/boost/preprocessor/iteration/detail/iter/forward1.hpp:62:0, /stuff/boost/utility/in_place_factory.hpp:24, /stuff/boost/optional/optional.hpp:37, /stuff/boost/optional.hpp:15, test.cpp:1:
you need in_place directly object:
delay_construct = boost::in_place<type>(params); note appears boost didn't support default construction through factories (nullary factories) until 1.35.
Comments
Post a Comment