28_ref_optional_semantics.qbk 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. [/
  2. Boost.Optional
  3. Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. [section Detailed Semantics - Optional Values]
  9. [note
  10. The following section contains various `assert()` which are used only to show
  11. the postconditions as sample code. It is not implied that the type `T` must
  12. support each particular expression but that if the expression is supported,
  13. the implied condition holds.
  14. ]
  15. __SPACE__
  16. [#reference_optional_constructor]
  17. [: `optional<T>::optional() noexcept;`]
  18. * [*Effect:] Default-Constructs an `optional`.
  19. * [*Postconditions:] `*this` is [_uninitialized].
  20. * [*Notes:] T's default constructor [_is not] called.
  21. * [*Example:]
  22. ``
  23. optional<T> def ;
  24. assert ( !def ) ;
  25. ``
  26. __SPACE__
  27. [#reference_optional_constructor_none_t]
  28. [: `optional<T>::optional( none_t ) noexcept;`]
  29. * [*Effect:] Constructs an `optional` uninitialized.
  30. * [*Postconditions:] `*this` is [_uninitialized].
  31. * [*Notes:] `T`'s default constructor [_is not] called. The expression
  32. `boost::none` denotes an instance of `boost::none_t` that can be used as
  33. the parameter.
  34. * [*Example:]
  35. ``
  36. #include <boost/none.hpp>
  37. optional<T> n(none) ;
  38. assert ( !n ) ;
  39. ``
  40. __SPACE__
  41. [#reference_optional_constructor_value]
  42. [: `optional<T>::optional( T const& v )`]
  43. * [*Requires:] `is_copy_constructible<T>::value` is `true`.
  44. * [*Effect:] Directly-Constructs an `optional`.
  45. * [*Postconditions:] `*this` is [_initialized] and its value is a ['copy]
  46. of `v`.
  47. * [*Throws:] Whatever `T::T( T const& )` throws.
  48. * [*Notes: ] `T::T( T const& )` is called.
  49. * [*Exception Safety:] Exceptions can only be thrown during
  50. `T::T( T const& );` in that case, this constructor has no effect.
  51. * [*Example:]
  52. ``
  53. T v;
  54. optional<T> opt(v);
  55. assert ( *opt == v ) ;
  56. ``
  57. __SPACE__
  58. [#reference_optional_constructor_move_value]
  59. [: `optional<T>::optional( T&& v )`]
  60. * [*Requires:] `is_move_constructible<T>::value` is `true`.
  61. * [*Effect:] Directly-Move-Constructs an `optional`.
  62. * [*Postconditions:] `*this` is [_initialized] and its value is move-constructed from `v`.
  63. * [*Throws:] Whatever `T::T( T&& )` throws.
  64. * [*Notes: ] `T::T( T&& )` is called.
  65. * [*Exception Safety:] Exceptions can only be thrown during
  66. `T::T( T&& );` in that case, the state of `v` is determined by exception safety guarantees for `T::T(T&&)`.
  67. * [*Example:]
  68. ``
  69. T v1, v2;
  70. optional<T> opt(std::move(v1));
  71. assert ( *opt == v2 ) ;
  72. ``
  73. __SPACE__
  74. [#reference_optional_constructor_bool_value]
  75. [: `optional<T>::optional( bool condition, T const& v ) ;` ]
  76. * If condition is true, same as:
  77. [: `optional<T>::optional( T const& v )`]
  78. * otherwise, same as:
  79. [: `optional<T>::optional()`]
  80. __SPACE__
  81. [#reference_optional_constructor_optional]
  82. [: `optional<T>::optional( optional const& rhs );`]
  83. * [*Requires:] `is_copy_constructible<T>::value` is `true`.
  84. * [*Effect:] Copy-Constructs an `optional`.
  85. * [*Postconditions:] If rhs is initialized, `*this` is initialized and
  86. its value is a ['copy] of the value of `rhs`; else `*this` is uninitialized.
  87. * [*Throws:] Whatever `T::T( T const& )` throws.
  88. * [*Notes:] If rhs is initialized, `T::T(T const& )` is called.
  89. * [*Exception Safety:] Exceptions can only be thrown during
  90. `T::T( T const& );` in that case, this constructor has no effect.
  91. * [*Example:]
  92. ``
  93. optional<T> uninit ;
  94. assert (!uninit);
  95. optional<T> uinit2 ( uninit ) ;
  96. assert ( uninit2 == uninit );
  97. optional<T> init( T(2) );
  98. assert ( *init == T(2) ) ;
  99. optional<T> init2 ( init ) ;
  100. assert ( init2 == init ) ;
  101. ``
  102. __SPACE__
  103. [#reference_optional_move_constructor_optional]
  104. [: `optional<T>::optional( optional&& rhs ) noexcept(`['see below]`);`]
  105. * [*Requires:] `is_move_constructible<T>::value` is `true`.
  106. * [*Effect:] Move-constructs an `optional`.
  107. * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and
  108. its value is move constructed from `rhs`; else `*this` is uninitialized.
  109. * [*Throws:] Whatever `T::T( T&& )` throws.
  110. * [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value`.
  111. * [*Notes:] If `rhs` is initialized, `T::T( T && )` is called.
  112. * [*Exception Safety:] Exceptions can only be thrown during
  113. `T::T( T&& );` in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety of `T::T(T&&)`.
  114. * [*Example:]
  115. ``
  116. optional<std::unique_ptr<T>> uninit ;
  117. assert (!uninit);
  118. optional<std::unique_ptr<T>> uinit2 ( std::move(uninit) ) ;
  119. assert ( uninit2 == uninit );
  120. optional<std::unique_ptr<T>> init( std::uniqye_ptr<T>(new T(2)) );
  121. assert ( **init == T(2) ) ;
  122. optional<std::unique_ptr<T>> init2 ( std::move(init) ) ;
  123. assert ( init );
  124. assert ( *init == nullptr );
  125. assert ( init2 );
  126. assert ( **init2 == T(2) ) ;
  127. ``
  128. __SPACE__
  129. [#reference_optional_constructor_other_optional]
  130. [: `template<U> explicit optional<T>::optional( optional<U> const& rhs );`]
  131. * [*Effect:] Copy-Constructs an `optional`.
  132. * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its
  133. value is a ['copy] of the value of rhs converted to type `T`; else `*this` is
  134. uninitialized.
  135. * [*Throws:] Whatever `T::T( U const& )` throws.
  136. * [*Notes: ] `T::T( U const& )` is called if `rhs` is initialized, which requires a
  137. valid conversion from `U` to `T`.
  138. * [*Exception Safety:] Exceptions can only be thrown during `T::T( U const& );`
  139. in that case, this constructor has no effect.
  140. * [*Example:]
  141. ``
  142. optional<double> x(123.4);
  143. assert ( *x == 123.4 ) ;
  144. optional<int> y(x) ;
  145. assert( *y == 123 ) ;
  146. ``
  147. __SPACE__
  148. [#reference_optional_move_constructor_other_optional]
  149. [: `template<U> explicit optional<T>::optional( optional<U>&& rhs );`]
  150. * [*Effect:] Move-constructs an `optional`.
  151. * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its
  152. value is move-constructed from `*rhs`; else `*this` is
  153. uninitialized.
  154. * [*Throws:] Whatever `T::T( U&& )` throws.
  155. * [*Notes: ] `T::T( U&& )` is called if `rhs` is initialized, which requires a
  156. valid conversion from `U` to `T`.
  157. * [*Exception Safety:] Exceptions can only be thrown during `T::T( U&& );`
  158. in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety guarantee of `T::T( U&& )`.
  159. * [*Example:]
  160. ``
  161. optional<double> x(123.4);
  162. assert ( *x == 123.4 ) ;
  163. optional<int> y(std::move(x)) ;
  164. assert( *y == 123 ) ;
  165. ``
  166. __SPACE__
  167. [#reference_optional_in_place_init]
  168. [: `template<class... Args> explicit optional<T>::optional( in_place_init_t, Args&&... ars );`]
  169. * [*Requires:] `is_constructible_v<T, Args&&...>` is `true`.
  170. * [*Effect:] Initializes the contained value as if direct-non-list-initializing an object of type `T` with the
  171. arguments `std::forward<Args>(args)...`.
  172. * [*Postconditions:] `*this` is initialized.
  173. * [*Throws:] Any exception thrown by the selected constructor of `T`.
  174. * [*Notes: ] `T` need not be __MOVE_CONSTRUCTIBLE__. On compilers that do not suppor variadic templates or rvalue references, this constuctor is available in limited functionality. For details [link optional_emplace_workaround see here].
  175. * [*Example:]
  176. ``
  177. // creates an std::mutex using its default constructor
  178. optional<std::mutex> om {in_place_init};
  179. assert (om);
  180. // creates a unique_lock by calling unique_lock(*om, std::defer_lock)
  181. optional<std::unique_lock<std::mutex>> ol {in_place_init, *om, std::defer_lock};
  182. assert (ol);
  183. assert (!ol->owns_lock());
  184. ``
  185. __SPACE__
  186. [#reference_optional_in_place_init_if]
  187. [: `template<class... Args> explicit optional<T>::optional( in_place_init_if_t, bool condition, Args&&... ars );`]
  188. * [*Requires:] `is_constructible_v<T, Args&&...>` is `true`.
  189. * [*Effect:] If `condition` is `true`, initializes the contained value as if direct-non-list-initializing an object of type `T` with the arguments `std::forward<Args>(args)...`.
  190. * [*Postconditions:] `bool(*this) == condition`.
  191. * [*Throws:] Any exception thrown by the selected constructor of `T`.
  192. * [*Notes: ] `T` need not be __MOVE_CONSTRUCTIBLE__. On compilers that do not suppor variadic templates or rvalue references, this constuctor is available in limited functionality. For details [link optional_emplace_workaround see here].
  193. * [*Example:]
  194. ``
  195. optional<std::vector<std::string>> ov1 {in_place_init_if, false, 3, "A"};
  196. assert (!ov1);
  197. optional<std::vector<std::string>> ov2 {in_place_init_if, true, 3, "A"};
  198. assert (ov2);
  199. assert (ov2->size() == 3);
  200. ``
  201. __SPACE__
  202. [#reference_optional_constructor_factory]
  203. [: `template<InPlaceFactory> explicit optional<T>::optional( InPlaceFactory const& f );`]
  204. [: `template<TypedInPlaceFactory> explicit optional<T>::optional( TypedInPlaceFactory const& f );`]
  205. * [*Effect:] Constructs an `optional` with a value of `T` obtained from the
  206. factory.
  207. * [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given]
  208. from the factory `f` (i.e., the value [_is not copied]).
  209. * [*Throws:] Whatever the `T` constructor called by the factory throws.
  210. * [*Notes:] See [link boost_optional.tutorial.in_place_factories In-Place Factories]
  211. * [*Exception Safety:] Exceptions can only be thrown during the call to
  212. the `T` constructor used by the factory; in that case, this constructor has
  213. no effect.
  214. * [*Example:]
  215. ``
  216. class C { C ( char, double, std::string ) ; } ;
  217. C v('A',123.4,"hello");
  218. optional<C> x( in_place ('A', 123.4, "hello") ); // InPlaceFactory used
  219. optional<C> y( in_place<C>('A', 123.4, "hello") ); // TypedInPlaceFactory used
  220. assert ( *x == v ) ;
  221. assert ( *y == v ) ;
  222. ``
  223. __SPACE__
  224. [#reference_optional_operator_equal_none_t]
  225. [: `optional& optional<T>::operator= ( none_t ) noexcept;`]
  226. * [*Effect:] If `*this` is initialized destroys its contained value.
  227. * [*Postconditions: ] `*this` is uninitialized.
  228. __SPACE__
  229. [#reference_optional_operator_equal_value]
  230. [: `optional& optional<T>::operator= ( T const& rhs ) ;`]
  231. * [*Effect:] Assigns the value `rhs` to an `optional`.
  232. * [*Postconditions: ] `*this` is initialized and its value is a ['copy] of `rhs`.
  233. * [*Throws:] Whatever `T::operator=( T const& )` or `T::T(T const&)` throws.
  234. * [*Notes:] If `*this` was initialized, `T`'s assignment operator is used,
  235. otherwise, its copy-constructor is used.
  236. * [*Exception Safety:] In the event of an exception, the initialization
  237. state of `*this` is unchanged and its value unspecified as far as `optional`
  238. is concerned (it is up to `T`'s `operator=()`). If `*this` is initially
  239. uninitialized and `T`'s ['copy constructor] fails, `*this` is left properly
  240. uninitialized.
  241. * [*Example:]
  242. ``
  243. T x;
  244. optional<T> def ;
  245. optional<T> opt(x) ;
  246. T y;
  247. def = y ;
  248. assert ( *def == y ) ;
  249. opt = y ;
  250. assert ( *opt == y ) ;
  251. ``
  252. __SPACE__
  253. [#reference_optional_operator_move_equal_value]
  254. [: `optional& optional<T>::operator= ( T&& rhs ) ;`]
  255. * [*Effect:] Moves the value `rhs` to an `optional`.
  256. * [*Postconditions: ] `*this` is initialized and its value is moved from `rhs`.
  257. * [*Throws:] Whatever `T::operator=( T&& )` or `T::T(T &&)` throws.
  258. * [*Notes:] If `*this` was initialized, `T`'s move-assignment operator is used,
  259. otherwise, its move-constructor is used.
  260. * [*Exception Safety:] In the event of an exception, the initialization
  261. state of `*this` is unchanged and its value unspecified as far as `optional`
  262. is concerned (it is up to `T`'s `operator=()`). If `*this` is initially
  263. uninitialized and `T`'s ['move constructor] fails, `*this` is left properly
  264. uninitialized.
  265. * [*Example:]
  266. ``
  267. T x;
  268. optional<T> def ;
  269. optional<T> opt(x) ;
  270. T y1, y2, yR;
  271. def = std::move(y1) ;
  272. assert ( *def == yR ) ;
  273. opt = std::move(y2) ;
  274. assert ( *opt == yR ) ;
  275. ``
  276. __SPACE__
  277. [#reference_optional_operator_equal_optional]
  278. [: `optional& optional<T>::operator= ( optional const& rhs ) ;`]
  279. * [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `CopyAssignable`.
  280. * [*Effects:]
  281. [table
  282. []
  283. [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
  284. [[[*`rhs` contains a value]][assigns `*rhs` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `*rhs`]]
  285. [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
  286. ]
  287. * [*Returns:] `*this`;
  288. * [*Postconditions:] `bool(rhs) == bool(*this)`.
  289. * [*Exception Safety:] If any exception is thrown, the initialization state of `*this` and `rhs` remains unchanged.
  290. If an exception is thrown during the call to `T`'s copy constructor, no effect.
  291. If an exception is thrown during the call to `T`'s copy assignment, the state of its contained value is as defined by the exception safety guarantee of `T`'s copy assignment.
  292. * [*Example:]
  293. ``
  294. T v;
  295. optional<T> opt(v);
  296. optional<T> def ;
  297. opt = def ;
  298. assert ( !def ) ;
  299. // previous value (copy of 'v') destroyed from within 'opt'.
  300. ``
  301. __SPACE__
  302. [#reference_optional_operator_move_equal_optional]
  303. [: `optional& optional<T>::operator= ( optional&& rhs ) noexcept(`['see below]`);`]
  304. * [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `MoveAssignable`.
  305. * [*Effects:]
  306. [table
  307. []
  308. [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
  309. [[[*`rhs` contains a value]][assigns `std::move(*rhs)` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `std::move(*rhs)`]]
  310. [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
  311. ]
  312. * [*Returns:] `*this`;
  313. * [*Postconditions:] `bool(rhs) == bool(*this)`.
  314. * [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value`.
  315. * [*Exception Safety:] If any exception is thrown, the initialization state of `*this` and `rhs` remains unchanged. If an exception is
  316. thrown during the call to `T`'s move constructor, the state of `*rhs` is determined by the exception safety guarantee
  317. of `T`'s move constructor. If an exception is thrown during the call to T's move-assignment, the state of `**this` and `*rhs` is determined by the exception safety guarantee of T's move assignment.
  318. * [*Example:]
  319. ``
  320. optional<T> opt(T(2)) ;
  321. optional<T> def ;
  322. opt = def ;
  323. assert ( def ) ;
  324. assert ( opt ) ;
  325. assert ( *opt == T(2) ) ;
  326. ``
  327. __SPACE__
  328. [#reference_optional_operator_equal_other_optional]
  329. [: `template<U> optional& optional<T>::operator= ( optional<U> const& rhs ) ;`]
  330. * [*Effect:]
  331. [table
  332. []
  333. [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
  334. [[[*`rhs` contains a value]][assigns `*rhs` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `*rhs`]]
  335. [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
  336. ]
  337. * [*Returns:] `*this`.
  338. * [*Postconditions:] `bool(rhs) == bool(*this)`.
  339. * [*Exception Safety:] If any exception is thrown, the result of the expression `bool(*this)` remains unchanged.
  340. If an exception is thrown during the call to `T`'s constructor, no effect.
  341. If an exception is thrown during the call to `T`'s assignment, the state of its contained value is as defined by the exception safety guarantee of `T`'s copy assignment.
  342. * [*Example:]
  343. ``
  344. T v;
  345. optional<T> opt0(v);
  346. optional<U> opt1;
  347. opt1 = opt0 ;
  348. assert ( *opt1 == static_cast<U>(v) ) ;
  349. ``
  350. __SPACE__
  351. [#reference_optional_operator_move_equal_other_optional]
  352. [: `template<U> optional& optional<T>::operator= ( optional<U>&& rhs ) ;`]
  353. * [*Effect:]
  354. [table
  355. []
  356. [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
  357. [[[*`rhs` contains a value]][assigns `std::move(*rhs)` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `std::move(*rhs)`]]
  358. [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
  359. ]
  360. * [*Returns:] `*this`.
  361. * [*Postconditions:] `bool(rhs) == bool(*this)`.
  362. * [*Exception Safety:] If any exception is thrown, the result of the expression `bool(*this)` remains unchanged.
  363. If an exception is thrown during the call to `T`'s constructor, no effect.
  364. If an exception is thrown during the call to `T`'s assignment, the state of its contained value is as defined by the exception safety guarantee of `T`'s copy assignment.
  365. * [*Example:]
  366. ``
  367. T v;
  368. optional<T> opt0(v);
  369. optional<U> opt1;
  370. opt1 = std::move(opt0) ;
  371. assert ( opt0 );
  372. assert ( opt1 )
  373. assert ( *opt1 == static_cast<U>(v) ) ;
  374. ``
  375. __SPACE__
  376. [#reference_optional_emplace]
  377. [: `template<class... Args> void optional<T>::emplace( Args&&... args );`]
  378. * [*Requires:] The compiler supports rvalue references and variadic templates.
  379. * [*Effect:] If `*this` is initialized calls `*this = none`.
  380. Then initializes in-place the contained value as if direct-initializing an object
  381. of type `T` with `std::forward<Args>(args)...`.
  382. * [*Postconditions: ] `*this` is [_initialized].
  383. * [*Throws:] Whatever the selected `T`'s constructor throws.
  384. * [*Exception Safety:] If an exception is thrown during the initialization of `T`, `*this` is ['uninitialized].
  385. * [*Notes:] `T` need not be __MOVE_CONSTRUCTIBLE__ or `MoveAssignable`. On compilers that do not suppor variadic templates or rvalue references, this function is available in limited functionality. For details [link optional_emplace_workaround see here].
  386. * [*Example:]
  387. ``
  388. T v;
  389. optional<const T> opt;
  390. opt.emplace(0); // create in-place using ctor T(int)
  391. opt.emplace(); // destroy previous and default-construct another T
  392. opt.emplace(v); // destroy and copy-construct in-place (no assignment called)
  393. ``
  394. __SPACE__
  395. [#reference_optional_operator_equal_factory]
  396. [: `template<InPlaceFactory> optional<T>& optional<T>::operator=( InPlaceFactory const& f );`]
  397. [: `template<TypedInPlaceFactory> optional<T>& optional<T>::operator=( TypedInPlaceFactory const& f );`]
  398. * [*Effect:] Assigns an `optional` with a value of `T` obtained from the
  399. factory.
  400. * [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given]
  401. from the factory `f` (i.e., the value [_is not copied]).
  402. * [*Throws:] Whatever the `T` constructor called by the factory throws.
  403. * [*Notes:] See [link boost_optional.tutorial.in_place_factories In-Place Factories]
  404. * [*Exception Safety:] Exceptions can only be thrown during the call to
  405. the `T` constructor used by the factory; in that case, the `optional` object
  406. will be reset to be ['uninitialized].
  407. __SPACE__
  408. [#reference_optional_reset_value]
  409. [: `void optional<T>::reset( T const& v ) ;`]
  410. * [*Deprecated:] same as `operator= ( T const& v) ;`
  411. __SPACE__
  412. [#reference_optional_reset]
  413. [: `void optional<T>::reset() noexcept ;`]
  414. * [*Effects:] Same as `operator=( none_t );`
  415. __SPACE__
  416. [#reference_optional_get]
  417. [: `T const& optional<T>::get() const ;`]
  418. [: `T& optional<T>::get() ;`]
  419. [: `inline T const& get ( optional<T> const& ) ;`]
  420. [: `inline T& get ( optional<T> &) ;`]
  421. * [*Requires:] `*this` is initialized
  422. * [*Returns:] A reference to the contained value
  423. * [*Throws:] Nothing.
  424. * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`.
  425. __SPACE__
  426. [#reference_optional_operator_asterisk]
  427. [: `T const& optional<T>::operator*() const& ;`]
  428. [: `T& optional<T>::operator*() &;`]
  429. * [*Requires:] `*this` is initialized
  430. * [*Returns:] A reference to the contained value
  431. * [*Throws:] Nothing.
  432. * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`. On compilers that do not support ref-qualifiers on member functions these two overloads are replaced with the classical two: a `const` and non-`const` member functions.
  433. * [*Example:]
  434. ``
  435. T v ;
  436. optional<T> opt ( v );
  437. T const& u = *opt;
  438. assert ( u == v ) ;
  439. T w ;
  440. *opt = w ;
  441. assert ( *opt == w ) ;
  442. ``
  443. __SPACE__
  444. [#reference_optional_operator_asterisk_move]
  445. [: `T&& optional<T>::operator*() &&;`]
  446. * [*Requires:] `*this` contains a value.
  447. * [*Effects:] Equivalent to `return std::move(*val);`.
  448. * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`. On compilers that do not support ref-qualifiers on member functions this overload is not present.
  449. __SPACE__
  450. [#reference_optional_value]
  451. [: `T const& optional<T>::value() const& ;`]
  452. [: `T& optional<T>::value() & ;`]
  453. * [*Effects:] Equivalent to `return bool(*this) ? *val : throw bad_optional_access();`.
  454. * [*Notes:] On compilers that do not support ref-qualifiers on member functions these two overloads are replaced with the classical two: a `const` and non-`const` member functions.
  455. * [*Example:]
  456. ``
  457. T v ;
  458. optional<T> o0, o1 ( v );
  459. assert ( o1.value() == v );
  460. try {
  461. o0.value(); // throws
  462. assert ( false );
  463. }
  464. catch(bad_optional_access&) {
  465. assert ( true );
  466. }
  467. ``
  468. __SPACE__
  469. [#reference_optional_value_move]
  470. [: `T&& optional<T>::value() && ;`]
  471. * [*Effects:] Equivalent to `return bool(*this) ? std::move(*val) : throw bad_optional_access();`.
  472. * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
  473. __SPACE__
  474. [#reference_optional_value_or]
  475. [: `template<class U> T optional<T>::value_or(U && v) const& ;`]
  476. * [*Effects:] Equivalent to `if (*this) return **this; else return std::forward<U>(v);`.
  477. * [*Remarks:] If `T` is not __COPY_CONSTRUCTIBLE__ or `U &&` is not convertible to `T`, the program is ill-formed.
  478. * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function. On compilers without rvalue reference support the type of `v` becomes `U const&`.
  479. __SPACE__
  480. [#reference_optional_value_or_move]
  481. [: `template<class U> T optional<T>::value_or(U && v) && ;`]
  482. * [*Effects:] Equivalent to `if (*this) return std::move(**this); else return std::forward<U>(v);`.
  483. * [*Remarks:] If `T` is not __MOVE_CONSTRUCTIBLE__ or `U &&` is not convertible to `T`, the program is ill-formed.
  484. * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
  485. __SPACE__
  486. [#reference_optional_value_or_call]
  487. [: `template<class F> T optional<T>::value_or_eval(F f) const& ;`]
  488. * [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`.
  489. * [*Effects:] `if (*this) return **this; else return f();`.
  490. * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function.
  491. * [*Example:]
  492. ``
  493. int complain_and_0()
  494. {
  495. clog << "no value returned, using default" << endl;
  496. return 0;
  497. }
  498. optional<int> o1 = 1;
  499. optional<int> oN = none;
  500. int i = o1.value_or_eval(complain_and_0); // fun not called
  501. assert (i == 1);
  502. int j = oN.value_or_eval(complain_and_0); // fun called
  503. assert (i == 0);
  504. ``
  505. __SPACE__
  506. [#reference_optional_value_or_call_move]
  507. [: `template<class F> T optional<T>::value_or_eval(F f) && ;`]
  508. * [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`.
  509. * [*Effects:] `if (*this) return std::move(**this); else return f();`.
  510. * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
  511. __SPACE__
  512. [#reference_optional_map]
  513. [: `template<class F> auto optional<T>::map(F f) const& -> `['see below]` ;`]
  514. [: `template<class F> auto optional<T>::map(F f) & -> `['see below]` ;`]
  515. * [*Effects:] `if (*this) return f(**this); else return none;`
  516. * [*Notes:] The return type of these overloads is `optional<decltype(f(**this))>`. On compilers that do not support ref-qualifiers on member functions, these two (as well as the next one) overloads are replaced with good old const and non-const overloads.
  517. * [*Example:]
  518. ``
  519. auto length = [](const string& s){ return s.size(); };
  520. optional<string> o1 {}, o2 {"cat"};
  521. optional<size_t> os1 = o1.map(length), os2 = o2.map(length);
  522. assert ( !os1 ) ;
  523. assert ( os2 ) ;
  524. assert ( *os2 == 3 ) ;
  525. ``
  526. __SPACE__
  527. [#reference_optional_map_move]
  528. [: `template<class F> auto optional<T>::map(F f) && -> `['see below]` ;`]
  529. * [*Effects:] `if (*this) return f(std::move(**this)); else return none;`
  530. * [*Notes:] The return type of this overload is `optional<decltype(f(istd::move(**this)))>`.
  531. __SPACE__
  532. [#reference_optional_flat_map]
  533. [: `template<class F> auto optional<T>::flat_map(F f) const& -> `['see below]` ;`]
  534. [: `template<class F> auto optional<T>::flat_map(F f) & -> `['see below]` ;`]
  535. * [*Requires:] The return type of expression `f(**this)` is `optional<U>` for some object or reference type `U`.
  536. * [*Effects:] `if (*this) return f(**this); else return none;`
  537. * [*Notes:] The return type of these overloads is `optional<U>`. On compilers that do not support ref-qualifiers on member functions, these two (as well as the next one) overloads are replaced with good old const and non-const overloads.
  538. * [*Example:]
  539. ``
  540. optional<char> first_char(const string& s) {
  541. return s.empty() ? none : optional<char>(s[0]);
  542. };
  543. optional<string> o1 {}, o2 {"cat"};
  544. optional<char> os1 = o1.flat_map(first_char), os2 = o2.flat_map(first_char);
  545. assert ( !os1 ) ;
  546. assert ( os2 ) ;
  547. assert ( *os2 == 'c' ) ;
  548. ``
  549. __SPACE__
  550. [#reference_optional_flat_map_move]
  551. [: `template<class F> auto optional<T>::flat_map(F f) && -> `['see below]` ;`]
  552. * [*Requires:] The return type of expression `f(std::move(**this))` is `optional<U>` for some object or reference type `U`.
  553. * [*Effects:] `if (*this) return f(std::move(**this)); else return none;`
  554. * [*Notes:] The return type of this overload is `optional<U>`.
  555. __SPACE__
  556. [#reference_optional_get_value_or_value]
  557. [: `T const& optional<T>::get_value_or( T const& default) const ;`]
  558. [: `T& optional<T>::get_value_or( T& default ) ;`]
  559. * [*Deprecated:] Use `value_or()` instead.
  560. * [*Returns:] A reference to the contained value, if any, or `default`.
  561. * [*Throws:] Nothing.
  562. * [*Example:]
  563. ``
  564. T v, z ;
  565. optional<T> def;
  566. T const& y = def.get_value_or(z);
  567. assert ( y == z ) ;
  568. optional<T> opt ( v );
  569. T const& u = opt.get_value_or(z);
  570. assert ( u == v ) ;
  571. assert ( u != z ) ;
  572. ``
  573. __SPACE__
  574. [#reference_optional_get_ptr]
  575. [: `T const* optional<T>::get_ptr() const ;`]
  576. [: `T* optional<T>::get_ptr() ;`]
  577. * [*Returns:] If `*this` is initialized, a pointer to the contained value;
  578. else `0` (['null]).
  579. * [*Throws:] Nothing.
  580. * [*Notes:] The contained value is permanently stored within `*this`, so you
  581. should not hold nor delete this pointer
  582. * [*Example:]
  583. ``
  584. T v;
  585. optional<T> opt(v);
  586. optional<T> const copt(v);
  587. T* p = opt.get_ptr() ;
  588. T const* cp = copt.get_ptr();
  589. assert ( p == get_pointer(opt) );
  590. assert ( cp == get_pointer(copt) ) ;
  591. ``
  592. __SPACE__
  593. [#reference_optional_operator_arrow]
  594. [: `T const* optional<T>::operator ->() const ;`]
  595. [: `T* optional<T>::operator ->() ;`]
  596. * [*Requires: ] `*this` is initialized.
  597. * [*Returns:] A pointer to the contained value.
  598. * [*Throws:] Nothing.
  599. * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`.
  600. * [*Example:]
  601. ``
  602. struct X { int mdata ; } ;
  603. X x ;
  604. optional<X> opt (x);
  605. opt->mdata = 2 ;
  606. ``
  607. __SPACE__
  608. [#reference_optional_operator_bool]
  609. [: `explicit optional<T>::operator bool() const noexcept ;`]
  610. [: `bool optional<T>::has_value() const noexcept ;`]
  611. * [*Returns:] `get_ptr() != 0`.
  612. * [*Notes:] On compilers that do not support explicit conversion operators this falls back to safe-bool idiom.
  613. * [*Example:]
  614. ``
  615. optional<T> def ;
  616. assert ( def == 0 );
  617. optional<T> opt ( v ) ;
  618. assert ( opt );
  619. assert ( opt != 0 );
  620. ``
  621. __SPACE__
  622. [#reference_optional_operator_not]
  623. [: `bool optional<T>::operator!() noexcept ;`]
  624. * [*Returns:] If `*this` is uninitialized, `true`; else `false`.
  625. * [*Notes:] This operator is provided for those compilers which can't
  626. use the ['unspecified-bool-type operator] in certain boolean contexts.
  627. * [*Example:]
  628. ``
  629. optional<T> opt ;
  630. assert ( !opt );
  631. *opt = some_T ;
  632. // Notice the "double-bang" idiom here.
  633. assert ( !!opt ) ;
  634. ``
  635. __SPACE__
  636. [#reference_optional_is_initialized]
  637. [: `bool optional<T>::is_initialized() const ;`]
  638. * [*Deprecated:] Same as `explicit operator bool () ;`
  639. [endsect]
  640. [section Detailed Semantics - Optional References]
  641. __SPACE__
  642. [#reference_optional_ref_default_ctor]
  643. [: `optional<T&>::optional() noexcept;`]
  644. [: `optional<T&>::optional(none_t) noexcept;`]
  645. * [*Postconditions:] `bool(*this) == false`; `*this` refers to nothing.
  646. __SPACE__
  647. [#reference_optional_ref_value_ctor]
  648. [: `template<class R> optional<T&>::optional(R&& r) noexcept;`]
  649. * [*Postconditions:] `bool(*this) == true`; `addressof(**this) == addressof(r)`.
  650. * [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This constructor does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
  651. * [*Notes:] This constructor is declared `explicit` on compilers that do not correctly suport binding to const lvalues of integral types. For more details [link optional_reference_binding see here].
  652. * [*Example:]
  653. ``
  654. T v;
  655. T& vref = v ;
  656. optional<T&> opt(vref);
  657. assert ( *opt == v ) ;
  658. ++ v ; // mutate referee
  659. assert (*opt == v);
  660. ``
  661. __SPACE__
  662. [#reference_optional_ref_cond_value_ctor]
  663. [: `template<class R> optional<T&>::optional(bool cond, R&& r) noexcept;`]
  664. * [*Effects: ] Initializes `ref` with expression `cond ? addressof(r) : nullptr`.
  665. * [*Postconditions:] `bool(*this) == cond`; If `bool(*this)`, `addressof(**this) == addressof(r)`.
  666. * [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This constructor does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
  667. __SPACE__
  668. [#reference_optional_ref_copy_ctor]
  669. [: `optional<T&>::optional ( optional const& rhs ) noexcept ;`]
  670. * [*Effects: ] Initializes `ref` with expression `rhs.ref`.
  671. * [*Postconditions:] `bool(*this) == bool(rhs)`.
  672. * [*Example:]
  673. ``
  674. optional<T&> uninit ;
  675. assert (!uninit);
  676. optional<T&> uinit2 ( uninit ) ;
  677. assert ( uninit2 == uninit );
  678. T v = 2 ; T& ref = v ;
  679. optional<T> init(ref);
  680. assert ( *init == v ) ;
  681. optional<T> init2 ( init ) ;
  682. assert ( *init2 == v ) ;
  683. v = 3 ;
  684. assert ( *init == 3 ) ;
  685. assert ( *init2 == 3 ) ;
  686. ``
  687. __SPACE__
  688. [#reference_optional_ref_ctor_from_opt_U]
  689. [: `template<class U> explicit optional<T&>::optional ( optional<U&> const& rhs ) noexcept ;`]
  690. * [*Requires:] `is_convertible<U&, T&>::value` is `true`.
  691. * [*Effects: ] Initializes `ref` with expression `rhs.ref`.
  692. * [*Postconditions:] `bool(*this) == bool(rhs)`.
  693. __SPACE__
  694. [#reference_optional_ref_assign_none_t]
  695. [: `optional<T&>::operator= ( none_t ) noexcept ;`]
  696. * [*Effects: ] Assigns `ref` with expression `nullptr`.
  697. * [*returns:] `*this`.
  698. * [*Postconditions:] `bool(*this) == false`.
  699. [#reference_optional_ref_copy_assign]
  700. [: `optional& optional<T&>::operator= ( optional const& rhs ) noexcept ;`]
  701. * [*Effects: ] Assigns `ref` with expression `rhs.ref`.
  702. * [*returns:] `*this`.
  703. * [*Postconditions:] `bool(*this) == bool(rhs)`.
  704. * [*Notes:] This behaviour is called ['rebinding semantics]. See [link boost_optional.tutorial.optional_references.rebinding_semantics_for_assignment_of_optional_references here] for details.
  705. * [*Example:]
  706. ``
  707. int a = 1 ;
  708. int b = 2 ;
  709. T& ra = a ;
  710. T& rb = b ;
  711. optional<int&> def ;
  712. optional<int&> ora(ra) ;
  713. optional<int&> orb(rb) ;
  714. def = orb ; // binds 'def' to 'b' through 'rb' wrapped within 'orb'
  715. assert ( *def == b ) ;
  716. *def = ora ; // changes the value of 'b' to a copy of the value of 'a'
  717. assert ( b == a ) ;
  718. int c = 3;
  719. int& rc = c ;
  720. optional<int&> orc(rc) ;
  721. ora = orc ; // REBINDS ora to 'c' through 'rc'
  722. c = 4 ;
  723. assert ( *ora == 4 ) ;
  724. ``
  725. [#reference_optional_ref_assign_optional_U]
  726. [: `template<class U> optional& optional<T&>::operator= ( optional<U&> const& rhs ) noexcept ;`]
  727. * [*Requires:] `is_convertible<U&, T&>::value` is `true`.
  728. * [*Effects: ] Assigns `ref` with expression `rhs.ref`.
  729. * [*returns:] `*this`.
  730. * [*Postconditions:] `bool(*this) == bool(rhs)`.
  731. __SPACE__
  732. [#reference_optional_ref_assign_R]
  733. [: `template<class R> optional& optional<T&>::operator= ( R&& r ) noexcept ;`]
  734. * [*Effects: ] Assigns `ref` with expression `r`.
  735. * [*returns:] `*this`.
  736. * [*Postconditions:] `bool(*this) == true`.
  737. * [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This function does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
  738. * [*Example:]
  739. ``
  740. int a = 1 ;
  741. int b = 2 ;
  742. T& ra = a ;
  743. T& rb = b ;
  744. optional<int&> def ;
  745. optional<int&> opt(ra) ;
  746. def = rb ; // binds 'def' to 'b' through 'rb'
  747. assert ( *def == b ) ;
  748. *def = a ; // changes the value of 'b' to a copy of the value of 'a'
  749. assert ( b == a ) ;
  750. int c = 3;
  751. int& rc = c ;
  752. opt = rc ; // REBINDS to 'c' through 'rc'
  753. c = 4 ;
  754. assert ( *opt == 4 ) ;
  755. ``
  756. __SPACE__
  757. [#reference_optional_ref_emplace_R]
  758. [: `void optional<T&>::emplace( R&& r ) noexcept ;`]
  759. * [*Effects: ] Assigns `ref` with expression `r`.
  760. * [*Postconditions:] `bool(*this) == true`.
  761. * [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This function does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
  762. __SPACE__
  763. [#reference_optional_ref_get]
  764. [: `T& optional<T&>::get() const ;`]
  765. [: `T& optional<T&>::operator *() const ;`]
  766. * [*Requires:] `bool(*this) == true`.
  767. * [*Effects: ] Returns `*ref`.
  768. * [*Throws: ] Nothing.
  769. * [*Example:]
  770. ``
  771. T v ;
  772. T& vref = v ;
  773. optional<T&> opt ( vref );
  774. T const& vref2 = *opt;
  775. assert ( vref2 == v ) ;
  776. ++ v ;
  777. assert ( *opt == v ) ;
  778. ``
  779. __SPACE__
  780. [#reference_optional_ref_arrow]
  781. [: `T* optional<T&>::operator -> () const ;`]
  782. * [*Requires:] `bool(*this) == true`.
  783. * [*Effects: ] Returns `ref`.
  784. * [*Throws: ] Nothing.
  785. __SPACE__
  786. [#reference_optional_ref_value]
  787. [: `T& optional<T&>::value() const ;`]
  788. * [*Effects:] Equivalent to `return bool(*this) ? *val : throw bad_optional_access();`.
  789. __SPACE__
  790. [#reference_optional_ref_value_or]
  791. [: `template<class R> T& optional<T&>::value_or( R&& r ) const noexcept;`]
  792. * [*Effects:] Equivalent to `if (*this) return **this; else return r;`.
  793. * [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed.
  794. __SPACE__
  795. [#reference_optional_ref_value_or_eval]
  796. [: `template<class F> T& optional<T&>::value_or( F f ) const ;`]
  797. * [*Effects:] Equivalent to `if (*this) return **this; else return f();`.
  798. * [*Remarks:] Unless `decltype(f())` is an lvalue reference, the program is ill-formed.
  799. __SPACE__
  800. [#reference_optional_ref_map]
  801. [: `template<class F> auto optional<T&>::map( F f ) const -> `['see below]`;`]
  802. * [*Effects:] Equivalent to `if (*this) return f(**this); else return none;`.
  803. * [*Remarks:] The return type of this function is `optional<decltype(f(**this))>`.
  804. __SPACE__
  805. [#reference_optional_ref_flat_map]
  806. [: `template<class F> auto optional<T&>::flat_map( F f ) const -> `['see below]`;`]
  807. * [*Requires:] The return type of expression `f(**this)` is `optional<U>` for some object or reference type `U`.
  808. * [*Effects:] Equivalent to `if (*this) return f(**this); else return none;`.
  809. * [*Remarks:] The return type of this function is `optional<U>`.
  810. __SPACE__
  811. [#reference_optional_ref_get_ptr]
  812. [: `T* optional<T&>::get_ptr () const noexcept;`]
  813. * [*Returns:] `ref`.
  814. __SPACE__
  815. [#reference_optional_ref_operator_bool]
  816. [: `bool has_value() const noexcept;`]
  817. [: `optional<T&>::operator bool () const noexcept;`]
  818. * [*Returns:] `bool(ref)`.
  819. __SPACE__
  820. [#reference_optional_ref_operator_not]
  821. [: `optional<T&>::operator ! () const noexcept;`]
  822. * [*Returns:] `!bool(ref)`.
  823. __SPACE__
  824. [#reference_optional_ref_reset]
  825. [: `void optional<T&>::reset() noexcept;`]
  826. * [*Effects:] Same as `*this = none`.
  827. __SPACE__
  828. [#reference_optional_ref_reset_value]
  829. [: `template<class R> void optional<T&>::reset ( R&& r) noexcept;`]
  830. * [*Effects:] Equivalent to `*this = std::forward<R>(r)`.
  831. * [*Remarks:] This function is depprecated.
  832. __SPACE__
  833. [#reference_optional_ref_is_initialized]
  834. [: `bool optional<T&>::is_initialized() const noexcept;`]
  835. * [*Effects:] Equivalent to `return bool(*this)`.
  836. * [*Remarks:] This function is depprecated.
  837. __SPACE__
  838. [#reference_optional_ref_get_value_or_value]
  839. [: `template<class R> T& optional<T&>::get_value_or( R&& r ) const noexcept;`]
  840. * [*Effects:] Equivalent to `return value_or(std::forward<R>(r);`.
  841. * [*Remarks:] This function is depprecated.
  842. [endsect]
  843. [section Detailed Semantics - Free Functions]
  844. __SPACE__
  845. [#reference_make_optional_value]
  846. [: `optional<T> make_optional( T const& v )`]
  847. * [*Returns: ] `optional<T>(v)` for the ['deduced] type `T` of `v`.
  848. * [*Example:]
  849. ``
  850. template<class T> void foo ( optional<T> const& opt ) ;
  851. foo ( make_optional(1+1) ) ; // Creates an optional<int>
  852. ``
  853. __SPACE__
  854. [#reference_make_optional_rvalue]
  855. [: `optional<std::decay_t<T>> make_optional( T && v )`]
  856. * [*Returns: ] `optional<std::decay_t<T>>(std::move(v))` for the ['deduced] type `T` of `v`.
  857. __SPACE__
  858. [#reference_make_optional_bool_value]
  859. [: `optional<T> make_optional( bool condition, T const& v )`]
  860. * [*Returns: ] `optional<T>(condition, v)` for the ['deduced] type `T` of `v`.
  861. * [*Example:]
  862. ``
  863. optional<double> calculate_foo()
  864. {
  865. double val = compute_foo();
  866. return make_optional(is_not_nan_and_finite(val),val);
  867. }
  868. optional<double> v = calculate_foo();
  869. if ( !v )
  870. error("foo wasn't computed");
  871. ``
  872. __SPACE__
  873. [#reference_make_optional_bool_rvalue]
  874. [: `optional<std::decay_t<T>> make_optional( bool condition, T && v )`]
  875. * [*Returns: ] `optional<std::decay_t<T>>(condition, std::move(v))` for the ['deduced] type `T` of `v`.
  876. __SPACE__
  877. [#reference_operator_compare_equal_optional_optional]
  878. [: `bool operator == ( optional<T> const& x, optional<T> const& y );`]
  879. * [*Requires:] `T` shall meet requirements of __SGI_EQUALITY_COMPARABLE__.
  880. * [*Returns:] If both `x` and `y` are initialized, `(*x == *y)`. If only
  881. `x` or `y` is initialized, `false`. If both are uninitialized, `true`.
  882. * [*Notes:] This definition guarantees that `optional<T>` not containing a value is compared unequal to any `optional<T>` containing any value, and equal to any other `optional<T>` not containing a value.
  883. Pointers have shallow relational operators while `optional` has deep relational operators. Do not use `operator==` directly in generic code which expect to be given either an `optional<T>` or a pointer; use
  884. __FUNCTION_EQUAL_POINTEES__ instead
  885. * [*Example:]
  886. ``
  887. optional<T> oN, oN_;
  888. optional<T> o1(T(1)), o1_(T(1));
  889. optional<T> o2(T(2));
  890. assert ( oN == oN ); // Identity implies equality
  891. assert ( o1 == o1 ); //
  892. assert ( oN == oN_ ); // Both uninitialized compare equal
  893. assert ( oN != o1 ); // Initialized unequal to initialized.
  894. assert ( o1 == o1_ ); // Both initialized compare as (*lhs == *rhs)
  895. assert ( o1 != o2 ); //
  896. ``
  897. __SPACE__
  898. [#reference_operator_compare_less_optional_optional]
  899. [: `bool operator < ( optional<T> const& x, optional<T> const& y );`]
  900. * [*Requires:] Expression `*x < *y` shall be well-formed and its result shall be convertible to `bool`.
  901. * [*Returns:] `(!y) ? false : (!x) ? true : *x < *y`.
  902. * [*Notes:] This definition guarantees that `optional<T>` not containing a value is ordered as less than any `optional<T>` containing any value, and equivalent to any other `optional<T>` not containing a value.
  903. Pointers have shallow relational operators while `optional` has deep relational operators. Do not use `operator<` directly in generic code
  904. which expect to be given either an `optional<T>` or a pointer; use __FUNCTION_LESS_POINTEES__ instead. `T` need not be __SGI_LESS_THAN_COMPARABLE__. Only single `operator<` is required. Other relational operations are defined in terms of this one. If `T`'s `operator<` satisfies the axioms of __SGI_LESS_THAN_COMPARABLE__ (transitivity, antisymmetry and irreflexivity), `optinal<T>` is __SGI_LESS_THAN_COMPARABLE__.
  905. * [*Example:]
  906. ``
  907. optional<T> oN, oN_;
  908. optional<T> o0(T(0));
  909. optional<T> o1(T(1));
  910. assert ( !(oN < oN) ); // Identity implies equivalence
  911. assert ( !(o1 < o1) );
  912. assert ( !(oN < oN_) ); // Two uninitialized are equivalent
  913. assert ( !(oN_ < oN) );
  914. assert ( oN < o0 ); // Uninitialized is less than initialized
  915. assert ( !(o0 < oN) );
  916. assert ( o1 < o2 ) ; // Two initialized compare as (*lhs < *rhs)
  917. assert ( !(o2 < o1) ) ;
  918. assert ( !(o2 < o2) ) ;
  919. ``
  920. __SPACE__
  921. [#reference_operator_compare_not_equal_optional_optional]
  922. [: `bool operator != ( optional<T> const& x, optional<T> const& y );`]
  923. * [*Returns: ] `!( x == y );`
  924. __SPACE__
  925. [#reference_operator_compare_greater_optional_optional]
  926. [: `bool operator > ( optional<T> const& x, optional<T> const& y );`]
  927. * [*Returns: ] `( y < x );`
  928. __SPACE__
  929. [#reference_operator_compare_less_or_equal_optional_optional]
  930. [: `bool operator <= ( optional<T> const& x, optional<T> const& y );`]
  931. * [*Returns: ] `!( y < x );`
  932. __SPACE__
  933. [#reference_operator_compare_greater_or_equal_optional_optional]
  934. [: `bool operator >= ( optional<T> const& x, optional<T> const& y );`]
  935. * [*Returns: ] `!( x < y );`
  936. __SPACE__
  937. [#reference_operator_compare_equal_optional_none]
  938. [: `bool operator == ( optional<T> const& x, none_t ) noexcept;`]
  939. [: `bool operator == ( none_t, optional<T> const& x ) noexcept;`]
  940. * [*Returns:] `!x`.
  941. * [*Notes:] `T` need not meet requirements of __SGI_EQUALITY_COMPARABLE__.
  942. __SPACE__
  943. [#reference_operator_compare_not_equal_optional_none]
  944. [: `bool operator != ( optional<T> const& x, none_t ) noexcept;`]
  945. [: `bool operator != ( none_t, optional<T> const& x ) noexcept;`]
  946. * [*Returns: ] `bool(x);`
  947. __SPACE__
  948. [#reference_free_get_pointer]
  949. [: `auto get_pointer ( optional<T>& o ) -> typename optional<T>::pointer_type ;`]
  950. [: `auto get_pointer ( optional<T> const& o ) -> typename optional<T>::pointer_const_type ;`]
  951. * [*Returns:] `o.get_ptr()`.
  952. * [*Throws:] Nothing.
  953. __SPACE__
  954. [#reference_free_get_value_or]
  955. [: `auto get_optional_value_or ( optional<T>& o, typename optional<T>::reference_type def ) -> typename optional<T>::reference_type ;`]
  956. [: `auto get_optional_value_or ( optional<T> const& o, typename optional<T>::reference_const_type def ) -> typename optional<T>::reference_const_type ;`]
  957. * [*Returns:] `o.get_value_or(def)`.
  958. * [*Throws:] Nothing.
  959. * [*Remarks:] This function is deprecated.
  960. __SPACE__
  961. [#reference_swap_optional_optional]
  962. [: `void swap ( optional<T>& x, optional<T>& y ) ;`]
  963. * [*Requires:] Lvalues of type `T` shall be swappable and `T` shall be __MOVE_CONSTRUCTIBLE__.
  964. * [*Effects:]
  965. [table
  966. []
  967. [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
  968. [[[*`rhs` contains a value]][calls `swap(*(*this), *rhs)`][initializes the contained value of `*this` as if direct-initializing an object of type `T` with the expression `std::move(*rhs)`, followed by `rhs.val->T::~T()`, `*this` contains a value and `rhs` does not contain a value]]
  969. [[[*`rhs` does not contain a value]][initializes the contained value of `rhs` as if direct-initializing an object of type `T` with the expression `std::move(*(*this))`, followed by `val->T::~T()`, `*this` does not contain a value and `rhs` contains a value][no effect]]
  970. ]
  971. * [*Postconditions:] The states of `x` and `y` interchanged.
  972. * [*Throws:] If both are initialized, whatever `swap(T&,T&)` throws. If only
  973. one is initialized, whatever `T::T ( T&& )` throws.
  974. * [*Example:]
  975. ``
  976. T x(12);
  977. T y(21);
  978. optional<T> def0 ;
  979. optional<T> def1 ;
  980. optional<T> optX(x);
  981. optional<T> optY(y);
  982. boost::swap(def0,def1); // no-op
  983. boost::swap(def0,optX);
  984. assert ( *def0 == x );
  985. assert ( !optX );
  986. boost::swap(def0,optX); // Get back to original values
  987. boost::swap(optX,optY);
  988. assert ( *optX == y );
  989. assert ( *optY == x );
  990. ``
  991. __SPACE__
  992. [#reference_swap_optional_reference]
  993. [: `void swap ( optional<T&>& x, optional<T&>& y ) noexcept ;`]
  994. * [*Postconditions:] `x` refers to what `y` refererred to before the swap (if anything). `y` refers to whatever `x` referred to before the swap.
  995. * [*Example:]
  996. ``
  997. T x(12);
  998. T y(21);
  999. optional<T&> opt0;
  1000. optional<T&> optX (x);
  1001. optional<T&> optY (y);
  1002. boost::swap(optX, optY);
  1003. assert (addressof(*optX) == addressof(y));
  1004. assert (addressof(*optY) == addressof(x));
  1005. boost::swap(opt0, optX);
  1006. assert ( opt0 );
  1007. assert ( !optX );
  1008. assert (addressof(*opt0) == addressof(y));
  1009. ``
  1010. [endsect]