Смотрите ниже код,
Вопрос в том, как я могу отложить создание объекта, который нельзя скопировать, используя<>.
В примере я использую Boost :: Optional, хотя я верю, что теперь он также в стандарте std :: Option.
Да, я мог бы использовать 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:
Вам нужно вставить in_place непосредственно в объект:
delay_construct = boost::in_place<Type>(params);
Обратите внимание, что похоже, что boost не поддерживал строительство по умолчанию через фабрики (нулевые фабрики) до 1.35.
В C ++ 14 у нас будет std::optional
, который случается иметь std::optional::emplace
, Так что вы можете сделать:
std::optional<HardFoo> delay_construct;
delay_construct.emplace(constructor_arg_1, constructor_arg_2);