optional.hpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2014 - 2021 Andrzej Krzemienski.
  3. //
  4. // Use, modification, and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/optional for documentation.
  9. //
  10. // You are welcome to contact the author at:
  11. // [email protected]
  12. //
  13. // Revisions:
  14. // 27 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen
  15. // 05 May 2014 (Added move semantics) Andrzej Krzemienski
  16. //
  17. #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
  18. #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
  19. #include <new>
  20. #ifndef BOOST_NO_IOSTREAM
  21. #include <iosfwd>
  22. #endif // BOOST_NO_IOSTREAM
  23. #ifdef BOOST_OPTIONAL_DETAIL_USE_STD_TYPE_TRAITS
  24. # include <type_traits>
  25. #endif
  26. #include <boost/assert.hpp>
  27. #include <boost/core/addressof.hpp>
  28. #include <boost/core/enable_if.hpp>
  29. #include <boost/core/explicit_operator_bool.hpp>
  30. #include <boost/core/invoke_swap.hpp>
  31. #include <boost/optional/bad_optional_access.hpp>
  32. #include <boost/static_assert.hpp>
  33. #include <boost/throw_exception.hpp>
  34. #include <boost/type.hpp>
  35. #include <boost/type_traits/alignment_of.hpp>
  36. #include <boost/type_traits/conditional.hpp>
  37. #include <boost/type_traits/conjunction.hpp>
  38. #include <boost/type_traits/disjunction.hpp>
  39. #include <boost/type_traits/has_nothrow_constructor.hpp>
  40. #include <boost/type_traits/type_with_alignment.hpp>
  41. #include <boost/type_traits/remove_const.hpp>
  42. #include <boost/type_traits/remove_reference.hpp>
  43. #include <boost/type_traits/decay.hpp>
  44. #include <boost/type_traits/is_assignable.hpp>
  45. #include <boost/type_traits/is_base_of.hpp>
  46. #include <boost/type_traits/is_const.hpp>
  47. #include <boost/type_traits/is_constructible.hpp>
  48. #include <boost/type_traits/is_convertible.hpp>
  49. #include <boost/type_traits/is_lvalue_reference.hpp>
  50. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  51. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  52. #include <boost/type_traits/is_rvalue_reference.hpp>
  53. #include <boost/type_traits/is_same.hpp>
  54. #include <boost/type_traits/is_volatile.hpp>
  55. #include <boost/type_traits/is_scalar.hpp>
  56. #include <boost/move/utility.hpp>
  57. #include <boost/none.hpp>
  58. #include <boost/utility/compare_pointees.hpp>
  59. #include <boost/utility/result_of.hpp>
  60. #include <boost/optional/optional_fwd.hpp>
  61. #include <boost/optional/detail/optional_config.hpp>
  62. #include <boost/optional/detail/optional_factory_support.hpp>
  63. #include <boost/optional/detail/optional_aligned_storage.hpp>
  64. #include <boost/optional/detail/optional_hash.hpp>
  65. namespace boost { namespace optional_detail {
  66. template <typename T>
  67. struct optional_value_type
  68. {
  69. };
  70. template <typename T>
  71. struct optional_value_type< ::boost::optional<T> >
  72. {
  73. typedef T type;
  74. };
  75. }} // namespace boost::optional_detail
  76. #ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
  77. #include <boost/optional/detail/old_optional_implementation.hpp>
  78. #else
  79. namespace boost {
  80. namespace optional_ns {
  81. // a tag for in-place initialization of contained value
  82. struct in_place_init_t
  83. {
  84. struct init_tag{};
  85. explicit in_place_init_t(init_tag){}
  86. };
  87. const in_place_init_t in_place_init ((in_place_init_t::init_tag()));
  88. // a tag for conditional in-place initialization of contained value
  89. struct in_place_init_if_t
  90. {
  91. struct init_tag{};
  92. explicit in_place_init_if_t(init_tag){}
  93. };
  94. const in_place_init_if_t in_place_init_if ((in_place_init_if_t::init_tag()));
  95. } // namespace optional_ns
  96. using optional_ns::in_place_init_t;
  97. using optional_ns::in_place_init;
  98. using optional_ns::in_place_init_if_t;
  99. using optional_ns::in_place_init_if;
  100. namespace optional_detail {
  101. struct init_value_tag {};
  102. struct optional_tag {};
  103. template<class T>
  104. class optional_base : public optional_tag
  105. {
  106. private :
  107. typedef aligned_storage<T> storage_type ;
  108. typedef optional_base<T> this_type ;
  109. protected :
  110. typedef T value_type ;
  111. typedef typename boost::remove_const<T>::type unqualified_value_type;
  112. protected:
  113. typedef T & reference_type ;
  114. typedef T const& reference_const_type ;
  115. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  116. typedef T && rval_reference_type ;
  117. typedef T && reference_type_of_temporary_wrapper ;
  118. #endif
  119. typedef T * pointer_type ;
  120. typedef T const* pointer_const_type ;
  121. typedef T const& argument_type ;
  122. // Creates an optional<T> uninitialized.
  123. // No-throw
  124. optional_base()
  125. :
  126. m_initialized(false) {}
  127. // Creates an optional<T> uninitialized.
  128. // No-throw
  129. optional_base ( none_t )
  130. :
  131. m_initialized(false) {}
  132. // Creates an optional<T> initialized with 'val'.
  133. // Can throw if T::T(T const&) does
  134. optional_base ( init_value_tag, argument_type val )
  135. :
  136. m_initialized(false)
  137. {
  138. construct(val);
  139. }
  140. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  141. // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
  142. // Can throw if T::T(T&&) does
  143. optional_base ( init_value_tag, rval_reference_type val )
  144. :
  145. m_initialized(false)
  146. {
  147. construct( boost::move(val) );
  148. }
  149. #endif
  150. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional<T>.
  151. // Can throw if T::T(T const&) does
  152. optional_base ( bool cond, argument_type val )
  153. :
  154. m_initialized(false)
  155. {
  156. if ( cond )
  157. construct(val);
  158. }
  159. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  160. // Creates an optional<T> initialized with 'move(val)' IFF cond is true, otherwise creates an uninitialized optional<T>.
  161. // Can throw if T::T(T &&) does
  162. optional_base ( bool cond, rval_reference_type val )
  163. :
  164. m_initialized(false)
  165. {
  166. if ( cond )
  167. construct(boost::move(val));
  168. }
  169. #endif
  170. // Creates a deep copy of another optional<T>
  171. // Can throw if T::T(T const&) does
  172. optional_base ( optional_base const& rhs )
  173. :
  174. m_initialized(false)
  175. {
  176. if ( rhs.is_initialized() )
  177. construct(rhs.get_impl());
  178. }
  179. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  180. // Creates a deep move of another optional<T>
  181. // Can throw if T::T(T&&) does
  182. optional_base ( optional_base&& rhs )
  183. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  184. :
  185. m_initialized(false)
  186. {
  187. if ( rhs.is_initialized() )
  188. construct( boost::move(rhs.get_impl()) );
  189. }
  190. #endif
  191. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  192. template<class Expr, class PtrExpr>
  193. explicit optional_base ( Expr&& expr, PtrExpr const* tag )
  194. :
  195. m_initialized(false)
  196. {
  197. construct(boost::forward<Expr>(expr),tag);
  198. }
  199. #else
  200. // This is used for both converting and in-place constructions.
  201. // Derived classes use the 'tag' to select the appropriate
  202. // implementation (the correct 'construct()' overload)
  203. template<class Expr>
  204. explicit optional_base ( Expr const& expr, Expr const* tag )
  205. :
  206. m_initialized(false)
  207. {
  208. construct(expr,tag);
  209. }
  210. #endif
  211. optional_base& operator= ( optional_base const& rhs )
  212. {
  213. this->assign(rhs);
  214. return *this;
  215. }
  216. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  217. optional_base& operator= ( optional_base && rhs )
  218. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  219. {
  220. this->assign(static_cast<optional_base&&>(rhs));
  221. return *this;
  222. }
  223. #endif
  224. // No-throw (assuming T::~T() doesn't)
  225. ~optional_base() { destroy() ; }
  226. // Assigns from another optional<T> (deep-copies the rhs value)
  227. void assign ( optional_base const& rhs )
  228. {
  229. if (is_initialized())
  230. {
  231. if ( rhs.is_initialized() )
  232. assign_value(rhs.get_impl());
  233. else destroy();
  234. }
  235. else
  236. {
  237. if ( rhs.is_initialized() )
  238. construct(rhs.get_impl());
  239. }
  240. }
  241. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  242. // Assigns from another optional<T> (deep-moves the rhs value)
  243. void assign ( optional_base&& rhs )
  244. {
  245. if (is_initialized())
  246. {
  247. if ( rhs.is_initialized() )
  248. assign_value( boost::move(rhs.get_impl()) );
  249. else destroy();
  250. }
  251. else
  252. {
  253. if ( rhs.is_initialized() )
  254. construct(boost::move(rhs.get_impl()));
  255. }
  256. }
  257. #endif
  258. // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
  259. template<class U>
  260. void assign ( optional<U> const& rhs )
  261. {
  262. if (is_initialized())
  263. {
  264. if ( rhs.is_initialized() )
  265. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  266. assign_value( rhs.get() );
  267. #else
  268. assign_value( static_cast<value_type>(rhs.get()) );
  269. #endif
  270. else destroy();
  271. }
  272. else
  273. {
  274. if ( rhs.is_initialized() )
  275. #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
  276. construct(rhs.get());
  277. #else
  278. construct(static_cast<value_type>(rhs.get()));
  279. #endif
  280. }
  281. }
  282. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  283. // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
  284. template<class U>
  285. void assign ( optional<U>&& rhs )
  286. {
  287. typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
  288. if (is_initialized())
  289. {
  290. if ( rhs.is_initialized() )
  291. assign_value( static_cast<ref_type>(rhs.get()) );
  292. else destroy();
  293. }
  294. else
  295. {
  296. if ( rhs.is_initialized() )
  297. construct(static_cast<ref_type>(rhs.get()));
  298. }
  299. }
  300. #endif
  301. // Assigns from a T (deep-copies the rhs value)
  302. void assign ( argument_type val )
  303. {
  304. if (is_initialized())
  305. assign_value(val);
  306. else construct(val);
  307. }
  308. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  309. // Assigns from a T (deep-moves the rhs value)
  310. void assign ( rval_reference_type val )
  311. {
  312. if (is_initialized())
  313. assign_value( boost::move(val) );
  314. else construct( boost::move(val) );
  315. }
  316. #endif
  317. // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
  318. // No-throw (assuming T::~T() doesn't)
  319. void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
  320. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  321. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  322. template<class Expr, class ExprPtr>
  323. void assign_expr ( Expr&& expr, ExprPtr const* tag )
  324. {
  325. if (is_initialized())
  326. assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
  327. else construct(boost::forward<Expr>(expr),tag);
  328. }
  329. #else
  330. template<class Expr>
  331. void assign_expr ( Expr const& expr, Expr const* tag )
  332. {
  333. if (is_initialized())
  334. assign_expr_to_initialized(expr,tag);
  335. else construct(expr,tag);
  336. }
  337. #endif
  338. #endif
  339. public :
  340. // Destroys the current value, if any, leaving this UNINITIALIZED
  341. // No-throw (assuming T::~T() doesn't)
  342. void reset() BOOST_NOEXCEPT { destroy(); }
  343. // **DEPRECATED** Replaces the current value -if any- with 'val'
  344. void reset ( argument_type val ) { assign(val); }
  345. // Returns a pointer to the value if this is initialized, otherwise,
  346. // returns NULL.
  347. // No-throw
  348. pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
  349. pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
  350. bool is_initialized() const BOOST_NOEXCEPT { return m_initialized ; }
  351. protected :
  352. void construct ( argument_type val )
  353. {
  354. ::new (m_storage.address()) unqualified_value_type(val) ;
  355. m_initialized = true ;
  356. }
  357. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  358. void construct ( rval_reference_type val )
  359. {
  360. ::new (m_storage.address()) unqualified_value_type( boost::move(val) ) ;
  361. m_initialized = true ;
  362. }
  363. #endif
  364. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  365. // Constructs in-place
  366. // upon exception *this is always uninitialized
  367. template<class... Args>
  368. void construct ( in_place_init_t, Args&&... args )
  369. {
  370. ::new (m_storage.address()) unqualified_value_type( boost::forward<Args>(args)... ) ;
  371. m_initialized = true ;
  372. }
  373. template<class... Args>
  374. void emplace_assign ( Args&&... args )
  375. {
  376. destroy();
  377. construct(in_place_init, boost::forward<Args>(args)...);
  378. }
  379. template<class... Args>
  380. explicit optional_base ( in_place_init_t, Args&&... args )
  381. :
  382. m_initialized(false)
  383. {
  384. construct(in_place_init, boost::forward<Args>(args)...);
  385. }
  386. template<class... Args>
  387. explicit optional_base ( in_place_init_if_t, bool cond, Args&&... args )
  388. :
  389. m_initialized(false)
  390. {
  391. if ( cond )
  392. construct(in_place_init, boost::forward<Args>(args)...);
  393. }
  394. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  395. template<class Arg>
  396. void construct ( in_place_init_t, Arg&& arg )
  397. {
  398. ::new (m_storage.address()) unqualified_value_type( boost::forward<Arg>(arg) );
  399. m_initialized = true ;
  400. }
  401. void construct ( in_place_init_t )
  402. {
  403. ::new (m_storage.address()) unqualified_value_type();
  404. m_initialized = true ;
  405. }
  406. template<class Arg>
  407. void emplace_assign ( Arg&& arg )
  408. {
  409. destroy();
  410. construct(in_place_init, boost::forward<Arg>(arg)) ;
  411. }
  412. void emplace_assign ()
  413. {
  414. destroy();
  415. construct(in_place_init) ;
  416. }
  417. template<class Arg>
  418. explicit optional_base ( in_place_init_t, Arg&& arg )
  419. :
  420. m_initialized(false)
  421. {
  422. construct(in_place_init, boost::forward<Arg>(arg));
  423. }
  424. explicit optional_base ( in_place_init_t )
  425. :
  426. m_initialized(false)
  427. {
  428. construct(in_place_init);
  429. }
  430. template<class Arg>
  431. explicit optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
  432. :
  433. m_initialized(false)
  434. {
  435. if ( cond )
  436. construct(in_place_init, boost::forward<Arg>(arg));
  437. }
  438. explicit optional_base ( in_place_init_if_t, bool cond )
  439. :
  440. m_initialized(false)
  441. {
  442. if ( cond )
  443. construct(in_place_init);
  444. }
  445. #else
  446. template<class Arg>
  447. void construct ( in_place_init_t, const Arg& arg )
  448. {
  449. ::new (m_storage.address()) unqualified_value_type( arg );
  450. m_initialized = true ;
  451. }
  452. template<class Arg>
  453. void construct ( in_place_init_t, Arg& arg )
  454. {
  455. ::new (m_storage.address()) unqualified_value_type( arg );
  456. m_initialized = true ;
  457. }
  458. void construct ( in_place_init_t )
  459. {
  460. ::new (m_storage.address()) unqualified_value_type();
  461. m_initialized = true ;
  462. }
  463. template<class Arg>
  464. void emplace_assign ( const Arg& arg )
  465. {
  466. destroy();
  467. construct(in_place_init, arg);
  468. }
  469. template<class Arg>
  470. void emplace_assign ( Arg& arg )
  471. {
  472. destroy();
  473. construct(in_place_init, arg);
  474. }
  475. void emplace_assign ()
  476. {
  477. destroy();
  478. construct(in_place_init);
  479. }
  480. template<class Arg>
  481. explicit optional_base ( in_place_init_t, const Arg& arg )
  482. : m_initialized(false)
  483. {
  484. construct(in_place_init, arg);
  485. }
  486. template<class Arg>
  487. explicit optional_base ( in_place_init_t, Arg& arg )
  488. : m_initialized(false)
  489. {
  490. construct(in_place_init, arg);
  491. }
  492. explicit optional_base ( in_place_init_t )
  493. : m_initialized(false)
  494. {
  495. construct(in_place_init);
  496. }
  497. template<class Arg>
  498. explicit optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
  499. : m_initialized(false)
  500. {
  501. if ( cond )
  502. construct(in_place_init, arg);
  503. }
  504. template<class Arg>
  505. explicit optional_base ( in_place_init_if_t, bool cond, Arg& arg )
  506. : m_initialized(false)
  507. {
  508. if ( cond )
  509. construct(in_place_init, arg);
  510. }
  511. explicit optional_base ( in_place_init_if_t, bool cond )
  512. : m_initialized(false)
  513. {
  514. if ( cond )
  515. construct(in_place_init);
  516. }
  517. #endif
  518. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  519. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  520. // Constructs in-place using the given factory
  521. template<class Expr>
  522. void construct ( Expr&& factory, in_place_factory_base const* )
  523. {
  524. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  525. m_initialized = true ;
  526. }
  527. // Constructs in-place using the given typed factory
  528. template<class Expr>
  529. void construct ( Expr&& factory, typed_in_place_factory_base const* )
  530. {
  531. factory.apply(m_storage.address()) ;
  532. m_initialized = true ;
  533. }
  534. template<class Expr>
  535. void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
  536. {
  537. destroy();
  538. construct(factory,tag);
  539. }
  540. // Constructs in-place using the given typed factory
  541. template<class Expr>
  542. void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
  543. {
  544. destroy();
  545. construct(factory,tag);
  546. }
  547. #else
  548. // Constructs in-place using the given factory
  549. template<class Expr>
  550. void construct ( Expr const& factory, in_place_factory_base const* )
  551. {
  552. boost_optional_detail::construct<value_type>(factory, m_storage.address());
  553. m_initialized = true ;
  554. }
  555. // Constructs in-place using the given typed factory
  556. template<class Expr>
  557. void construct ( Expr const& factory, typed_in_place_factory_base const* )
  558. {
  559. factory.apply(m_storage.address()) ;
  560. m_initialized = true ;
  561. }
  562. template<class Expr>
  563. void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
  564. {
  565. destroy();
  566. construct(factory,tag);
  567. }
  568. // Constructs in-place using the given typed factory
  569. template<class Expr>
  570. void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
  571. {
  572. destroy();
  573. construct(factory,tag);
  574. }
  575. #endif
  576. #endif
  577. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  578. // Constructs using any expression implicitly convertible to the single argument
  579. // of a one-argument T constructor.
  580. // Converting constructions of optional<T> from optional<U> uses this function with
  581. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  582. template<class Expr>
  583. void construct ( Expr&& expr, void const* )
  584. {
  585. new (m_storage.address()) unqualified_value_type(boost::forward<Expr>(expr)) ;
  586. m_initialized = true ;
  587. }
  588. // Assigns using a form any expression implicitly convertible to the single argument
  589. // of a T's assignment operator.
  590. // Converting assignments of optional<T> from optional<U> uses this function with
  591. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  592. template<class Expr>
  593. void assign_expr_to_initialized ( Expr&& expr, void const* )
  594. {
  595. assign_value( boost::forward<Expr>(expr) );
  596. }
  597. #else
  598. // Constructs using any expression implicitly convertible to the single argument
  599. // of a one-argument T constructor.
  600. // Converting constructions of optional<T> from optional<U> uses this function with
  601. // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
  602. template<class Expr>
  603. void construct ( Expr const& expr, void const* )
  604. {
  605. new (m_storage.address()) unqualified_value_type(expr) ;
  606. m_initialized = true ;
  607. }
  608. // Assigns using a form any expression implicitly convertible to the single argument
  609. // of a T's assignment operator.
  610. // Converting assignments of optional<T> from optional<U> uses this function with
  611. // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
  612. template<class Expr>
  613. void assign_expr_to_initialized ( Expr const& expr, void const* )
  614. {
  615. assign_value(expr);
  616. }
  617. #endif
  618. #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  619. // BCB5.64 (and probably lower versions) workaround.
  620. // The in-place factories are supported by means of catch-all constructors
  621. // and assignment operators (the functions are parameterized in terms of
  622. // an arbitrary 'Expr' type)
  623. // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
  624. // to the 'Expr'-taking functions even though explicit overloads are present for them.
  625. // Thus, the following overload is needed to properly handle the case when the 'lhs'
  626. // is another optional.
  627. //
  628. // For VC<=70 compilers this workaround doesn't work because the compiler issues and error
  629. // instead of choosing the wrong overload
  630. //
  631. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  632. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  633. template<class Expr>
  634. void construct ( Expr&& expr, optional_tag const* )
  635. {
  636. if ( expr.is_initialized() )
  637. {
  638. // An exception can be thrown here.
  639. // It it happens, THIS will be left uninitialized.
  640. new (m_storage.address()) unqualified_value_type(boost::move(expr.get())) ;
  641. m_initialized = true ;
  642. }
  643. }
  644. #else
  645. // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
  646. template<class Expr>
  647. void construct ( Expr const& expr, optional_tag const* )
  648. {
  649. if ( expr.is_initialized() )
  650. {
  651. // An exception can be thrown here.
  652. // It it happens, THIS will be left uninitialized.
  653. new (m_storage.address()) unqualified_value_type(expr.get()) ;
  654. m_initialized = true ;
  655. }
  656. }
  657. #endif
  658. #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  659. void assign_value ( argument_type val ) { get_impl() = val; }
  660. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  661. void assign_value ( rval_reference_type val ) { get_impl() = static_cast<rval_reference_type>(val); }
  662. #endif
  663. void destroy()
  664. {
  665. if ( m_initialized )
  666. destroy_impl() ;
  667. }
  668. reference_const_type get_impl() const { return m_storage.ref() ; }
  669. reference_type get_impl() { return m_storage.ref() ; }
  670. pointer_const_type get_ptr_impl() const { return m_storage.ptr_ref(); }
  671. pointer_type get_ptr_impl() { return m_storage.ptr_ref(); }
  672. private :
  673. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1900))
  674. void destroy_impl ( ) { m_storage.ptr_ref()->~T() ; m_initialized = false ; }
  675. #else
  676. void destroy_impl ( ) { m_storage.ref().T::~T() ; m_initialized = false ; }
  677. #endif
  678. bool m_initialized ;
  679. storage_type m_storage ;
  680. } ;
  681. #include <boost/optional/detail/optional_trivially_copyable_base.hpp>
  682. // definition of metafunction is_optional_val_init_candidate
  683. template <typename U>
  684. struct is_optional_or_tag
  685. : boost::conditional< boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
  686. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, none_t>::value
  687. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_t>::value
  688. || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_if_t>::value,
  689. boost::true_type, boost::false_type>::type
  690. {};
  691. template <typename T, typename U>
  692. struct has_dedicated_constructor
  693. : boost::disjunction<is_optional_or_tag<U>, boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<U>::type> >
  694. {};
  695. template <typename U>
  696. struct is_in_place_factory
  697. : boost::disjunction< boost::is_base_of<boost::in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>,
  698. boost::is_base_of<boost::typed_in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type> >
  699. {};
  700. #if !defined(BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT)
  701. template <typename T, typename U>
  702. struct is_factory_or_constructible_to_T
  703. : boost::disjunction< is_in_place_factory<U>, boost::is_constructible<T, U&&> >
  704. {};
  705. template <typename T, typename U>
  706. struct is_optional_constructible : boost::is_constructible<T, U>
  707. {};
  708. #else
  709. template <typename, typename>
  710. struct is_factory_or_constructible_to_T : boost::true_type
  711. {};
  712. template <typename T, typename U>
  713. struct is_optional_constructible : boost::true_type
  714. {};
  715. #endif // is_convertible condition
  716. #if !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  717. // for is_assignable
  718. #if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
  719. // On some initial rvalue reference implementations GCC does it in a strange way,
  720. // preferring perfect-forwarding constructor to implicit copy constructor.
  721. template <typename T, typename U>
  722. struct is_opt_assignable
  723. : boost::conjunction<boost::is_convertible<U&&, T>, boost::is_assignable<T&, U&&> >
  724. {};
  725. #else
  726. template <typename T, typename U>
  727. struct is_opt_assignable
  728. : boost::conjunction<boost::is_convertible<U, T>, boost::is_assignable<T&, U> >
  729. {};
  730. #endif
  731. #else
  732. template <typename T, typename U>
  733. struct is_opt_assignable : boost::is_convertible<U, T>
  734. {};
  735. #endif
  736. template <typename T, typename U>
  737. struct is_factory_or_opt_assignable_to_T
  738. : boost::disjunction< is_in_place_factory<U>, is_opt_assignable<T, U> >
  739. {};
  740. template <typename T, typename U, bool = has_dedicated_constructor<T, U>::value>
  741. struct is_optional_val_init_candidate
  742. : boost::false_type
  743. {};
  744. template <typename T, typename U>
  745. struct is_optional_val_init_candidate<T, U, false>
  746. : is_factory_or_constructible_to_T<T, U>
  747. {};
  748. template <typename T, typename U, bool = has_dedicated_constructor<T, U>::value>
  749. struct is_optional_val_assign_candidate
  750. : boost::false_type
  751. {};
  752. template <typename T, typename U>
  753. struct is_optional_val_assign_candidate<T, U, false>
  754. : is_factory_or_opt_assignable_to_T<T, U>
  755. {};
  756. } // namespace optional_detail
  757. namespace optional_config {
  758. template <typename T>
  759. struct optional_uses_direct_storage_for
  760. : boost::conditional<(boost::is_scalar<T>::value && !boost::is_const<T>::value && !boost::is_volatile<T>::value)
  761. , boost::true_type, boost::false_type>::type
  762. {};
  763. } // namespace optional_config
  764. #ifndef BOOST_OPTIONAL_DETAIL_NO_DIRECT_STORAGE_SPEC
  765. # define BOOST_OPTIONAL_BASE_TYPE(T) boost::conditional< optional_config::optional_uses_direct_storage_for<T>::value, \
  766. optional_detail::tc_optional_base<T>, \
  767. optional_detail::optional_base<T> \
  768. >::type
  769. #else
  770. # define BOOST_OPTIONAL_BASE_TYPE(T) optional_detail::optional_base<T>
  771. #endif
  772. template<class T>
  773. class optional
  774. : public BOOST_OPTIONAL_BASE_TYPE(T)
  775. {
  776. typedef typename BOOST_OPTIONAL_BASE_TYPE(T) base ;
  777. public :
  778. typedef optional<T> this_type ;
  779. typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
  780. typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
  781. typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
  782. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  783. typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
  784. typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
  785. #endif
  786. typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
  787. typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
  788. typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
  789. // Creates an optional<T> uninitialized.
  790. // No-throw
  791. optional() BOOST_NOEXCEPT : base() {}
  792. // Creates an optional<T> uninitialized.
  793. // No-throw
  794. optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
  795. // Creates an optional<T> initialized with 'val'.
  796. // Can throw if T::T(T const&) does
  797. optional ( argument_type val ) : base(optional_detail::init_value_tag(), val) {}
  798. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  799. // Creates an optional<T> initialized with 'move(val)'.
  800. // Can throw if T::T(T &&) does
  801. optional ( rval_reference_type val ) : base(optional_detail::init_value_tag(), boost::forward<T>(val))
  802. {}
  803. #endif
  804. // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  805. // Can throw if T::T(T const&) does
  806. optional ( bool cond, argument_type val ) : base(cond,val) {}
  807. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  808. /// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
  809. // Can throw if T::T(T &&) does
  810. optional ( bool cond, rval_reference_type val ) : base( cond, boost::forward<T>(val) )
  811. {}
  812. #endif
  813. // NOTE: MSVC needs templated versions first
  814. // Creates a deep copy of another convertible optional<U>
  815. // Requires a valid conversion from U to T.
  816. // Can throw if T::T(U const&) does
  817. template<class U>
  818. explicit optional ( optional<U> const& rhs
  819. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  820. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U const&>, bool>::type = true
  821. #endif
  822. )
  823. :
  824. base()
  825. {
  826. if ( rhs.is_initialized() )
  827. this->construct(rhs.get());
  828. }
  829. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  830. // Creates a deep move of another convertible optional<U>
  831. // Requires a valid conversion from U to T.
  832. // Can throw if T::T(U&&) does
  833. template<class U>
  834. explicit optional ( optional<U> && rhs
  835. #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
  836. ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U>, bool>::type = true
  837. #endif
  838. )
  839. :
  840. base()
  841. {
  842. if ( rhs.is_initialized() )
  843. this->construct( boost::move(rhs.get()) );
  844. }
  845. #endif
  846. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  847. // Creates an optional<T> with an expression which can be either
  848. // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
  849. // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
  850. // (c) Any expression implicitly convertible to the single type
  851. // of a one-argument T's constructor.
  852. // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
  853. // even though explicit overloads are present for these.
  854. // Depending on the above some T ctor is called.
  855. // Can throw if the resolved T ctor throws.
  856. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  857. template<class Expr>
  858. explicit optional ( Expr&& expr,
  859. BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true
  860. )
  861. : base(boost::forward<Expr>(expr),boost::addressof(expr))
  862. {}
  863. #else
  864. template<class Expr>
  865. explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
  866. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  867. #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  868. // Creates a deep copy of another optional<T>
  869. // Can throw if T::T(T const&) does
  870. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  871. optional ( optional const& ) = default;
  872. #else
  873. optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
  874. #endif
  875. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  876. // Creates a deep move of another optional<T>
  877. // Can throw if T::T(T&&) does
  878. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  879. optional ( optional && ) = default;
  880. #else
  881. optional ( optional && rhs )
  882. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
  883. : base( boost::move(rhs) )
  884. {}
  885. #endif
  886. #endif
  887. #if BOOST_WORKAROUND(_MSC_VER, <= 1600)
  888. // On old MSVC compilers the implicitly declared dtor is not called
  889. ~optional() {}
  890. #endif
  891. #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  892. // Assigns from an expression. See corresponding constructor.
  893. // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
  894. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  895. template<class Expr>
  896. BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_assign_candidate<T, Expr>, optional&>::type
  897. operator= ( Expr&& expr )
  898. {
  899. this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
  900. return *this ;
  901. }
  902. #else
  903. template<class Expr>
  904. optional& operator= ( Expr const& expr )
  905. {
  906. this->assign_expr(expr,boost::addressof(expr));
  907. return *this ;
  908. }
  909. #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  910. #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
  911. // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
  912. // Requires a valid conversion from U to T.
  913. // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
  914. template<class U>
  915. optional& operator= ( optional<U> const& rhs )
  916. {
  917. this->assign(rhs);
  918. return *this ;
  919. }
  920. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  921. // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
  922. // Requires a valid conversion from U to T.
  923. // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
  924. template<class U>
  925. optional& operator= ( optional<U> && rhs )
  926. {
  927. this->assign(boost::move(rhs));
  928. return *this ;
  929. }
  930. #endif
  931. // Assigns from another optional<T> (deep-copies the rhs value)
  932. // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
  933. // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
  934. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  935. optional& operator= ( optional const& rhs ) = default;
  936. #else
  937. optional& operator= ( optional const& rhs )
  938. {
  939. this->assign( static_cast<base const&>(rhs) ) ;
  940. return *this ;
  941. }
  942. #endif
  943. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  944. // Assigns from another optional<T> (deep-moves the rhs value)
  945. #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
  946. optional& operator= ( optional && ) = default;
  947. #else
  948. optional& operator= ( optional && rhs )
  949. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  950. {
  951. this->assign( static_cast<base &&>(rhs) ) ;
  952. return *this ;
  953. }
  954. #endif
  955. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  956. #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  957. // Assigns from a T (deep-moves/copies the rhs value)
  958. template <typename T_>
  959. BOOST_DEDUCED_TYPENAME boost::enable_if<boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<T_>::type>, optional&>::type
  960. operator= ( T_&& val )
  961. {
  962. this->assign( boost::forward<T_>(val) ) ;
  963. return *this ;
  964. }
  965. #else
  966. // Assigns from a T (deep-copies the rhs value)
  967. // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
  968. optional& operator= ( argument_type val )
  969. {
  970. this->assign( val ) ;
  971. return *this ;
  972. }
  973. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  974. // Assigns from a T (deep-moves the rhs value)
  975. optional& operator= ( rval_reference_type val )
  976. {
  977. this->assign( boost::move(val) ) ;
  978. return *this ;
  979. }
  980. #endif
  981. #endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
  982. // Assigns from a "none"
  983. // Which destroys the current value, if any, leaving this UNINITIALIZED
  984. // No-throw (assuming T::~T() doesn't)
  985. optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
  986. {
  987. this->assign( none_ ) ;
  988. return *this ;
  989. }
  990. #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  991. // Constructs in-place
  992. // upon exception *this is always uninitialized
  993. template<class... Args>
  994. void emplace ( Args&&... args )
  995. {
  996. this->emplace_assign( boost::forward<Args>(args)... );
  997. }
  998. template<class... Args>
  999. explicit optional ( in_place_init_t, Args&&... args )
  1000. : base( in_place_init, boost::forward<Args>(args)... )
  1001. {}
  1002. template<class... Args>
  1003. explicit optional ( in_place_init_if_t, bool cond, Args&&... args )
  1004. : base( in_place_init_if, cond, boost::forward<Args>(args)... )
  1005. {}
  1006. #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1007. template<class Arg>
  1008. void emplace ( Arg&& arg )
  1009. {
  1010. this->emplace_assign( boost::forward<Arg>(arg) );
  1011. }
  1012. void emplace ()
  1013. {
  1014. this->emplace_assign();
  1015. }
  1016. template<class Args>
  1017. explicit optional ( in_place_init_t, Args&& args )
  1018. : base( in_place_init, boost::forward<Args>(args) )
  1019. {}
  1020. explicit optional ( in_place_init_t )
  1021. : base( in_place_init )
  1022. {}
  1023. template<class Args>
  1024. explicit optional ( in_place_init_if_t, bool cond, Args&& args )
  1025. : base( in_place_init_if, cond, boost::forward<Args>(args) )
  1026. {}
  1027. explicit optional ( in_place_init_if_t, bool cond )
  1028. : base( in_place_init_if, cond )
  1029. {}
  1030. #else
  1031. template<class Arg>
  1032. void emplace ( const Arg& arg )
  1033. {
  1034. this->emplace_assign( arg );
  1035. }
  1036. template<class Arg>
  1037. void emplace ( Arg& arg )
  1038. {
  1039. this->emplace_assign( arg );
  1040. }
  1041. void emplace ()
  1042. {
  1043. this->emplace_assign();
  1044. }
  1045. template<class Arg>
  1046. explicit optional ( in_place_init_t, const Arg& arg )
  1047. : base( in_place_init, arg )
  1048. {}
  1049. template<class Arg>
  1050. explicit optional ( in_place_init_t, Arg& arg )
  1051. : base( in_place_init, arg )
  1052. {}
  1053. explicit optional ( in_place_init_t )
  1054. : base( in_place_init )
  1055. {}
  1056. template<class Arg>
  1057. explicit optional ( in_place_init_if_t, bool cond, const Arg& arg )
  1058. : base( in_place_init_if, cond, arg )
  1059. {}
  1060. template<class Arg>
  1061. explicit optional ( in_place_init_if_t, bool cond, Arg& arg )
  1062. : base( in_place_init_if, cond, arg )
  1063. {}
  1064. explicit optional ( in_place_init_if_t, bool cond )
  1065. : base( in_place_init_if, cond )
  1066. {}
  1067. #endif
  1068. void swap( optional & arg )
  1069. BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
  1070. {
  1071. // allow for Koenig lookup
  1072. boost::core::invoke_swap(*this, arg);
  1073. }
  1074. // Returns a reference to the value if this is initialized, otherwise,
  1075. // the behaviour is UNDEFINED
  1076. // No-throw
  1077. reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  1078. reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
  1079. // Returns a copy of the value if this is initialized, 'v' otherwise
  1080. reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
  1081. reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
  1082. // Returns a pointer to the value if this is initialized, otherwise,
  1083. // the behaviour is UNDEFINED
  1084. // No-throw
  1085. pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  1086. pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
  1087. // Returns a reference to the value if this is initialized, otherwise,
  1088. // the behaviour is UNDEFINED
  1089. // No-throw
  1090. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1091. reference_const_type operator *() const& { return this->get() ; }
  1092. reference_type operator *() & { return this->get() ; }
  1093. reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
  1094. #else
  1095. reference_const_type operator *() const { return this->get() ; }
  1096. reference_type operator *() { return this->get() ; }
  1097. #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
  1098. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1099. reference_const_type value() const&
  1100. {
  1101. if (this->is_initialized())
  1102. return this->get() ;
  1103. else
  1104. throw_exception(bad_optional_access());
  1105. }
  1106. reference_type value() &
  1107. {
  1108. if (this->is_initialized())
  1109. return this->get() ;
  1110. else
  1111. throw_exception(bad_optional_access());
  1112. }
  1113. reference_type_of_temporary_wrapper value() &&
  1114. {
  1115. if (this->is_initialized())
  1116. return boost::move(this->get()) ;
  1117. else
  1118. throw_exception(bad_optional_access());
  1119. }
  1120. #else
  1121. reference_const_type value() const
  1122. {
  1123. if (this->is_initialized())
  1124. return this->get() ;
  1125. else
  1126. throw_exception(bad_optional_access());
  1127. }
  1128. reference_type value()
  1129. {
  1130. if (this->is_initialized())
  1131. return this->get() ;
  1132. else
  1133. throw_exception(bad_optional_access());
  1134. }
  1135. #endif
  1136. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1137. template <class U>
  1138. value_type value_or ( U&& v ) const&
  1139. {
  1140. if (this->is_initialized())
  1141. return get();
  1142. else
  1143. return boost::forward<U>(v);
  1144. }
  1145. template <class U>
  1146. value_type value_or ( U&& v ) &&
  1147. {
  1148. if (this->is_initialized())
  1149. return boost::move(get());
  1150. else
  1151. return boost::forward<U>(v);
  1152. }
  1153. #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1154. template <class U>
  1155. value_type value_or ( U&& v ) const
  1156. {
  1157. if (this->is_initialized())
  1158. return get();
  1159. else
  1160. return boost::forward<U>(v);
  1161. }
  1162. #else
  1163. template <class U>
  1164. value_type value_or ( U const& v ) const
  1165. {
  1166. if (this->is_initialized())
  1167. return get();
  1168. else
  1169. return v;
  1170. }
  1171. template <class U>
  1172. value_type value_or ( U& v ) const
  1173. {
  1174. if (this->is_initialized())
  1175. return get();
  1176. else
  1177. return v;
  1178. }
  1179. #endif
  1180. #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  1181. template <typename F>
  1182. value_type value_or_eval ( F f ) const&
  1183. {
  1184. if (this->is_initialized())
  1185. return get();
  1186. else
  1187. return f();
  1188. }
  1189. template <typename F>
  1190. value_type value_or_eval ( F f ) &&
  1191. {
  1192. if (this->is_initialized())
  1193. return boost::move(get());
  1194. else
  1195. return f();
  1196. }
  1197. template <typename F>
  1198. optional<typename boost::result_of<F(reference_type)>::type> map(F f) &
  1199. {
  1200. if (this->has_value())
  1201. return f(get());
  1202. else
  1203. return none;
  1204. }
  1205. template <typename F>
  1206. optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const&
  1207. {
  1208. if (this->has_value())
  1209. return f(get());
  1210. else
  1211. return none;
  1212. }
  1213. template <typename F>
  1214. optional<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type> map(F f) &&
  1215. {
  1216. if (this->has_value())
  1217. return f(boost::move(this->get()));
  1218. else
  1219. return none;
  1220. }
  1221. template <typename F>
  1222. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f) &
  1223. {
  1224. if (this->has_value())
  1225. return f(get());
  1226. else
  1227. return none;
  1228. }
  1229. template <typename F>
  1230. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const&
  1231. {
  1232. if (this->has_value())
  1233. return f(get());
  1234. else
  1235. return none;
  1236. }
  1237. template <typename F>
  1238. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type>::type> flat_map(F f) &&
  1239. {
  1240. if (this->has_value())
  1241. return f(boost::move(get()));
  1242. else
  1243. return none;
  1244. }
  1245. #else
  1246. template <typename F>
  1247. value_type value_or_eval ( F f ) const
  1248. {
  1249. if (this->is_initialized())
  1250. return get();
  1251. else
  1252. return f();
  1253. }
  1254. template <typename F>
  1255. optional<typename boost::result_of<F(reference_type)>::type> map(F f)
  1256. {
  1257. if (this->has_value())
  1258. return f(get());
  1259. else
  1260. return none;
  1261. }
  1262. template <typename F>
  1263. optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const
  1264. {
  1265. if (this->has_value())
  1266. return f(get());
  1267. else
  1268. return none;
  1269. }
  1270. template <typename F>
  1271. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f)
  1272. {
  1273. if (this->has_value())
  1274. return f(get());
  1275. else
  1276. return none;
  1277. }
  1278. template <typename F>
  1279. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const
  1280. {
  1281. if (this->has_value())
  1282. return f(get());
  1283. else
  1284. return none;
  1285. }
  1286. #endif
  1287. bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; }
  1288. bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
  1289. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  1290. } ;
  1291. } // namespace boost
  1292. #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
  1293. namespace boost {
  1294. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1295. template<class T>
  1296. class optional<T&&>
  1297. {
  1298. BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal.");
  1299. } ;
  1300. #endif
  1301. } // namespace boost
  1302. #ifndef BOOST_OPTIONAL_CONFIG_DONT_SPECIALIZE_OPTIONAL_REFS
  1303. # include <boost/optional/detail/optional_reference_spec.hpp>
  1304. #endif
  1305. namespace boost {
  1306. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1307. template<class T>
  1308. inline
  1309. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( T && v )
  1310. {
  1311. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(boost::forward<T>(v));
  1312. }
  1313. // Returns optional<T>(cond,v)
  1314. template<class T>
  1315. inline
  1316. optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( bool cond, T && v )
  1317. {
  1318. return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(cond,boost::forward<T>(v));
  1319. }
  1320. #else
  1321. // Returns optional<T>(v)
  1322. template<class T>
  1323. inline
  1324. optional<T> make_optional ( T const& v )
  1325. {
  1326. return optional<T>(v);
  1327. }
  1328. // Returns optional<T>(cond,v)
  1329. template<class T>
  1330. inline
  1331. optional<T> make_optional ( bool cond, T const& v )
  1332. {
  1333. return optional<T>(cond,v);
  1334. }
  1335. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  1336. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  1337. // No-throw
  1338. template<class T>
  1339. inline
  1340. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  1341. get ( optional<T> const& opt )
  1342. {
  1343. return opt.get() ;
  1344. }
  1345. template<class T>
  1346. inline
  1347. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  1348. get ( optional<T>& opt )
  1349. {
  1350. return opt.get() ;
  1351. }
  1352. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  1353. // No-throw
  1354. template<class T>
  1355. inline
  1356. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  1357. get ( optional<T> const* opt )
  1358. {
  1359. return opt->get_ptr() ;
  1360. }
  1361. template<class T>
  1362. inline
  1363. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  1364. get ( optional<T>* opt )
  1365. {
  1366. return opt->get_ptr() ;
  1367. }
  1368. // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
  1369. // No-throw
  1370. template<class T>
  1371. inline
  1372. BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
  1373. get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
  1374. {
  1375. return opt.get_value_or(v) ;
  1376. }
  1377. template<class T>
  1378. inline
  1379. BOOST_DEDUCED_TYPENAME optional<T>::reference_type
  1380. get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
  1381. {
  1382. return opt.get_value_or(v) ;
  1383. }
  1384. // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
  1385. // No-throw
  1386. template<class T>
  1387. inline
  1388. BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
  1389. get_pointer ( optional<T> const& opt )
  1390. {
  1391. return opt.get_ptr() ;
  1392. }
  1393. template<class T>
  1394. inline
  1395. BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
  1396. get_pointer ( optional<T>& opt )
  1397. {
  1398. return opt.get_ptr() ;
  1399. }
  1400. } // namespace boost
  1401. #ifndef BOOST_NO_IOSTREAM
  1402. namespace boost {
  1403. // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
  1404. template<class CharType, class CharTrait>
  1405. std::basic_ostream<CharType, CharTrait>&
  1406. operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&)
  1407. {
  1408. BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
  1409. return os;
  1410. }
  1411. } // namespace boost
  1412. #endif // BOOST_NO_IOSTREAM
  1413. #include <boost/optional/detail/optional_relops.hpp>
  1414. #include <boost/optional/detail/optional_swap.hpp>
  1415. #endif // header guard