Brian Silverman | af2eaa8 | 2018-08-04 17:28:31 -0700 | [diff] [blame^] | 1 | [/ |
| 2 | Boost.Optional |
| 3 | |
| 4 | Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal |
| 5 | |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | http://www.boost.org/LICENSE_1_0.txt) |
| 9 | ] |
| 10 | |
| 11 | |
| 12 | [section Detailed Semantics - Optional Values] |
| 13 | |
| 14 | [note |
| 15 | The following section contains various `assert()` which are used only to show |
| 16 | the postconditions as sample code. It is not implied that the type `T` must |
| 17 | support each particular expression but that if the expression is supported, |
| 18 | the implied condition holds. |
| 19 | ] |
| 20 | |
| 21 | |
| 22 | __SPACE__ |
| 23 | |
| 24 | [#reference_optional_constructor] |
| 25 | |
| 26 | [: `optional<T>::optional() noexcept;`] |
| 27 | |
| 28 | * [*Effect:] Default-Constructs an `optional`. |
| 29 | * [*Postconditions:] `*this` is [_uninitialized]. |
| 30 | * [*Notes:] T's default constructor [_is not] called. |
| 31 | * [*Example:] |
| 32 | `` |
| 33 | optional<T> def ; |
| 34 | assert ( !def ) ; |
| 35 | `` |
| 36 | |
| 37 | __SPACE__ |
| 38 | |
| 39 | [#reference_optional_constructor_none_t] |
| 40 | |
| 41 | [: `optional<T>::optional( none_t ) noexcept;`] |
| 42 | |
| 43 | * [*Effect:] Constructs an `optional` uninitialized. |
| 44 | * [*Postconditions:] `*this` is [_uninitialized]. |
| 45 | * [*Notes:] `T`'s default constructor [_is not] called. The expression |
| 46 | `boost::none` denotes an instance of `boost::none_t` that can be used as |
| 47 | the parameter. |
| 48 | * [*Example:] |
| 49 | `` |
| 50 | #include <boost/none.hpp> |
| 51 | optional<T> n(none) ; |
| 52 | assert ( !n ) ; |
| 53 | `` |
| 54 | |
| 55 | __SPACE__ |
| 56 | |
| 57 | [#reference_optional_constructor_value] |
| 58 | |
| 59 | [: `optional<T>::optional( T const& v )`] |
| 60 | |
| 61 | * [*Requires:] `is_copy_constructible<T>::value` is `true`. |
| 62 | * [*Effect:] Directly-Constructs an `optional`. |
| 63 | * [*Postconditions:] `*this` is [_initialized] and its value is a ['copy] |
| 64 | of `v`. |
| 65 | * [*Throws:] Whatever `T::T( T const& )` throws. |
| 66 | * [*Notes: ] `T::T( T const& )` is called. |
| 67 | * [*Exception Safety:] Exceptions can only be thrown during |
| 68 | `T::T( T const& );` in that case, this constructor has no effect. |
| 69 | * [*Example:] |
| 70 | `` |
| 71 | T v; |
| 72 | optional<T> opt(v); |
| 73 | assert ( *opt == v ) ; |
| 74 | `` |
| 75 | |
| 76 | |
| 77 | __SPACE__ |
| 78 | |
| 79 | [#reference_optional_constructor_move_value] |
| 80 | |
| 81 | [: `optional<T>::optional( T&& v )`] |
| 82 | |
| 83 | * [*Requires:] `is_move_constructible<T>::value` is `true`. |
| 84 | * [*Effect:] Directly-Move-Constructs an `optional`. |
| 85 | * [*Postconditions:] `*this` is [_initialized] and its value is move-constructed from `v`. |
| 86 | * [*Throws:] Whatever `T::T( T&& )` throws. |
| 87 | * [*Notes: ] `T::T( T&& )` is called. |
| 88 | * [*Exception Safety:] Exceptions can only be thrown during |
| 89 | `T::T( T&& );` in that case, the state of `v` is determined by exception safety guarantees for `T::T(T&&)`. |
| 90 | * [*Example:] |
| 91 | `` |
| 92 | T v1, v2; |
| 93 | optional<T> opt(std::move(v1)); |
| 94 | assert ( *opt == v2 ) ; |
| 95 | `` |
| 96 | |
| 97 | |
| 98 | __SPACE__ |
| 99 | |
| 100 | [#reference_optional_constructor_bool_value] |
| 101 | |
| 102 | [: `optional<T>::optional( bool condition, T const& v ) ;` ] |
| 103 | |
| 104 | * If condition is true, same as: |
| 105 | |
| 106 | [: `optional<T>::optional( T const& v )`] |
| 107 | |
| 108 | * otherwise, same as: |
| 109 | |
| 110 | [: `optional<T>::optional()`] |
| 111 | |
| 112 | |
| 113 | __SPACE__ |
| 114 | |
| 115 | [#reference_optional_constructor_optional] |
| 116 | |
| 117 | [: `optional<T>::optional( optional const& rhs );`] |
| 118 | |
| 119 | * [*Requires:] `is_copy_constructible<T>::value` is `true`. |
| 120 | * [*Effect:] Copy-Constructs an `optional`. |
| 121 | * [*Postconditions:] If rhs is initialized, `*this` is initialized and |
| 122 | its value is a ['copy] of the value of `rhs`; else `*this` is uninitialized. |
| 123 | * [*Throws:] Whatever `T::T( T const& )` throws. |
| 124 | * [*Notes:] If rhs is initialized, `T::T(T const& )` is called. |
| 125 | * [*Exception Safety:] Exceptions can only be thrown during |
| 126 | `T::T( T const& );` in that case, this constructor has no effect. |
| 127 | * [*Example:] |
| 128 | `` |
| 129 | optional<T> uninit ; |
| 130 | assert (!uninit); |
| 131 | |
| 132 | optional<T> uinit2 ( uninit ) ; |
| 133 | assert ( uninit2 == uninit ); |
| 134 | |
| 135 | optional<T> init( T(2) ); |
| 136 | assert ( *init == T(2) ) ; |
| 137 | |
| 138 | optional<T> init2 ( init ) ; |
| 139 | assert ( init2 == init ) ; |
| 140 | `` |
| 141 | |
| 142 | |
| 143 | __SPACE__ |
| 144 | |
| 145 | [#reference_optional_move_constructor_optional] |
| 146 | |
| 147 | [: `optional<T>::optional( optional&& rhs ) noexcept(`['see below]`);`] |
| 148 | |
| 149 | * [*Requires:] `is_move_constructible<T>::value` is `true`. |
| 150 | * [*Effect:] Move-constructs an `optional`. |
| 151 | * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and |
| 152 | its value is move constructed from `rhs`; else `*this` is uninitialized. |
| 153 | * [*Throws:] Whatever `T::T( T&& )` throws. |
| 154 | * [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value`. |
| 155 | * [*Notes:] If `rhs` is initialized, `T::T( T && )` is called. |
| 156 | * [*Exception Safety:] Exceptions can only be thrown during |
| 157 | `T::T( T&& );` in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety of `T::T(T&&)`. |
| 158 | * [*Example:] |
| 159 | `` |
| 160 | optional<std::unique_ptr<T>> uninit ; |
| 161 | assert (!uninit); |
| 162 | |
| 163 | optional<std::unique_ptr<T>> uinit2 ( std::move(uninit) ) ; |
| 164 | assert ( uninit2 == uninit ); |
| 165 | |
| 166 | optional<std::unique_ptr<T>> init( std::uniqye_ptr<T>(new T(2)) ); |
| 167 | assert ( **init == T(2) ) ; |
| 168 | |
| 169 | optional<std::unique_ptr<T>> init2 ( std::move(init) ) ; |
| 170 | assert ( init ); |
| 171 | assert ( *init == nullptr ); |
| 172 | assert ( init2 ); |
| 173 | assert ( **init2 == T(2) ) ; |
| 174 | `` |
| 175 | |
| 176 | |
| 177 | __SPACE__ |
| 178 | |
| 179 | [#reference_optional_constructor_other_optional] |
| 180 | |
| 181 | [: `template<U> explicit optional<T>::optional( optional<U> const& rhs );`] |
| 182 | |
| 183 | * [*Effect:] Copy-Constructs an `optional`. |
| 184 | * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its |
| 185 | value is a ['copy] of the value of rhs converted to type `T`; else `*this` is |
| 186 | uninitialized. |
| 187 | * [*Throws:] Whatever `T::T( U const& )` throws. |
| 188 | * [*Notes: ] `T::T( U const& )` is called if `rhs` is initialized, which requires a |
| 189 | valid conversion from `U` to `T`. |
| 190 | * [*Exception Safety:] Exceptions can only be thrown during `T::T( U const& );` |
| 191 | in that case, this constructor has no effect. |
| 192 | * [*Example:] |
| 193 | `` |
| 194 | optional<double> x(123.4); |
| 195 | assert ( *x == 123.4 ) ; |
| 196 | |
| 197 | optional<int> y(x) ; |
| 198 | assert( *y == 123 ) ; |
| 199 | `` |
| 200 | |
| 201 | __SPACE__ |
| 202 | |
| 203 | [#reference_optional_move_constructor_other_optional] |
| 204 | |
| 205 | [: `template<U> explicit optional<T>::optional( optional<U>&& rhs );`] |
| 206 | |
| 207 | * [*Effect:] Move-constructs an `optional`. |
| 208 | * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its |
| 209 | value is move-constructed from `*rhs`; else `*this` is |
| 210 | uninitialized. |
| 211 | * [*Throws:] Whatever `T::T( U&& )` throws. |
| 212 | * [*Notes: ] `T::T( U&& )` is called if `rhs` is initialized, which requires a |
| 213 | valid conversion from `U` to `T`. |
| 214 | * [*Exception Safety:] Exceptions can only be thrown during `T::T( U&& );` |
| 215 | in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety guarantee of `T::T( U&& )`. |
| 216 | * [*Example:] |
| 217 | `` |
| 218 | optional<double> x(123.4); |
| 219 | assert ( *x == 123.4 ) ; |
| 220 | |
| 221 | optional<int> y(std::move(x)) ; |
| 222 | assert( *y == 123 ) ; |
| 223 | `` |
| 224 | |
| 225 | __SPACE__ |
| 226 | |
| 227 | [#reference_optional_in_place_init] |
| 228 | |
| 229 | [: `template<class... Args> explicit optional<T>::optional( in_place_init_t, Args&&... ars );`] |
| 230 | |
| 231 | * [*Requires:] `is_constructible_v<T, Args&&...>` is `true`. |
| 232 | * [*Effect:] Initializes the contained value as if direct-non-list-initializing an object of type `T` with the |
| 233 | arguments `std::forward<Args>(args)...`. |
| 234 | * [*Postconditions:] `*this` is initialized. |
| 235 | * [*Throws:] Any exception thrown by the selected constructor of `T`. |
| 236 | * [*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]. |
| 237 | |
| 238 | * [*Example:] |
| 239 | `` |
| 240 | // creates an std::mutex using its default constructor |
| 241 | optional<std::mutex> om {in_place_init}; |
| 242 | assert (om); |
| 243 | |
| 244 | // creates a unique_lock by calling unique_lock(*om, std::defer_lock) |
| 245 | optional<std::unique_lock<std::mutex>> ol {in_place_init, *om, std::defer_lock}; |
| 246 | assert (ol); |
| 247 | assert (!ol->owns_lock()); |
| 248 | `` |
| 249 | |
| 250 | __SPACE__ |
| 251 | |
| 252 | [#reference_optional_in_place_init_if] |
| 253 | |
| 254 | [: `template<class... Args> explicit optional<T>::optional( in_place_init_if_t, bool condition, Args&&... ars );`] |
| 255 | |
| 256 | * [*Requires:] `is_constructible_v<T, Args&&...>` is `true`. |
| 257 | * [*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)...`. |
| 258 | * [*Postconditions:] `bool(*this) == condition`. |
| 259 | * [*Throws:] Any exception thrown by the selected constructor of `T`. |
| 260 | * [*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]. |
| 261 | |
| 262 | * [*Example:] |
| 263 | `` |
| 264 | optional<std::vector<std::string>> ov1 {in_place_init_if, false, 3, "A"}; |
| 265 | assert (!ov1); |
| 266 | |
| 267 | optional<std::vector<std::string>> ov2 {in_place_init_if, true, 3, "A"}; |
| 268 | assert (ov2); |
| 269 | assert (ov2->size() == 3); |
| 270 | `` |
| 271 | |
| 272 | __SPACE__ |
| 273 | |
| 274 | [#reference_optional_constructor_factory] |
| 275 | |
| 276 | [: `template<InPlaceFactory> explicit optional<T>::optional( InPlaceFactory const& f );`] |
| 277 | [: `template<TypedInPlaceFactory> explicit optional<T>::optional( TypedInPlaceFactory const& f );`] |
| 278 | |
| 279 | * [*Effect:] Constructs an `optional` with a value of `T` obtained from the |
| 280 | factory. |
| 281 | * [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given] |
| 282 | from the factory `f` (i.e., the value [_is not copied]). |
| 283 | * [*Throws:] Whatever the `T` constructor called by the factory throws. |
| 284 | * [*Notes:] See [link boost_optional.tutorial.in_place_factories In-Place Factories] |
| 285 | * [*Exception Safety:] Exceptions can only be thrown during the call to |
| 286 | the `T` constructor used by the factory; in that case, this constructor has |
| 287 | no effect. |
| 288 | * [*Example:] |
| 289 | `` |
| 290 | class C { C ( char, double, std::string ) ; } ; |
| 291 | |
| 292 | C v('A',123.4,"hello"); |
| 293 | |
| 294 | optional<C> x( in_place ('A', 123.4, "hello") ); // InPlaceFactory used |
| 295 | optional<C> y( in_place<C>('A', 123.4, "hello") ); // TypedInPlaceFactory used |
| 296 | |
| 297 | assert ( *x == v ) ; |
| 298 | assert ( *y == v ) ; |
| 299 | `` |
| 300 | |
| 301 | __SPACE__ |
| 302 | |
| 303 | [#reference_optional_operator_equal_none_t] |
| 304 | |
| 305 | [: `optional& optional<T>::operator= ( none_t ) noexcept;`] |
| 306 | |
| 307 | * [*Effect:] If `*this` is initialized destroys its contained value. |
| 308 | * [*Postconditions: ] `*this` is uninitialized. |
| 309 | |
| 310 | __SPACE__ |
| 311 | |
| 312 | [#reference_optional_operator_equal_value] |
| 313 | |
| 314 | [: `optional& optional<T>::operator= ( T const& rhs ) ;`] |
| 315 | |
| 316 | * [*Effect:] Assigns the value `rhs` to an `optional`. |
| 317 | * [*Postconditions: ] `*this` is initialized and its value is a ['copy] of `rhs`. |
| 318 | * [*Throws:] Whatever `T::operator=( T const& )` or `T::T(T const&)` throws. |
| 319 | * [*Notes:] If `*this` was initialized, `T`'s assignment operator is used, |
| 320 | otherwise, its copy-constructor is used. |
| 321 | * [*Exception Safety:] In the event of an exception, the initialization |
| 322 | state of `*this` is unchanged and its value unspecified as far as `optional` |
| 323 | is concerned (it is up to `T`'s `operator=()`). If `*this` is initially |
| 324 | uninitialized and `T`'s ['copy constructor] fails, `*this` is left properly |
| 325 | uninitialized. |
| 326 | * [*Example:] |
| 327 | `` |
| 328 | T x; |
| 329 | optional<T> def ; |
| 330 | optional<T> opt(x) ; |
| 331 | |
| 332 | T y; |
| 333 | def = y ; |
| 334 | assert ( *def == y ) ; |
| 335 | opt = y ; |
| 336 | assert ( *opt == y ) ; |
| 337 | `` |
| 338 | |
| 339 | |
| 340 | __SPACE__ |
| 341 | |
| 342 | [#reference_optional_operator_move_equal_value] |
| 343 | |
| 344 | [: `optional& optional<T>::operator= ( T&& rhs ) ;`] |
| 345 | |
| 346 | * [*Effect:] Moves the value `rhs` to an `optional`. |
| 347 | * [*Postconditions: ] `*this` is initialized and its value is moved from `rhs`. |
| 348 | * [*Throws:] Whatever `T::operator=( T&& )` or `T::T(T &&)` throws. |
| 349 | * [*Notes:] If `*this` was initialized, `T`'s move-assignment operator is used, |
| 350 | otherwise, its move-constructor is used. |
| 351 | * [*Exception Safety:] In the event of an exception, the initialization |
| 352 | state of `*this` is unchanged and its value unspecified as far as `optional` |
| 353 | is concerned (it is up to `T`'s `operator=()`). If `*this` is initially |
| 354 | uninitialized and `T`'s ['move constructor] fails, `*this` is left properly |
| 355 | uninitialized. |
| 356 | * [*Example:] |
| 357 | `` |
| 358 | T x; |
| 359 | optional<T> def ; |
| 360 | optional<T> opt(x) ; |
| 361 | |
| 362 | T y1, y2, yR; |
| 363 | def = std::move(y1) ; |
| 364 | assert ( *def == yR ) ; |
| 365 | opt = std::move(y2) ; |
| 366 | assert ( *opt == yR ) ; |
| 367 | `` |
| 368 | |
| 369 | |
| 370 | __SPACE__ |
| 371 | |
| 372 | [#reference_optional_operator_equal_optional] |
| 373 | |
| 374 | [: `optional& optional<T>::operator= ( optional const& rhs ) ;`] |
| 375 | |
| 376 | * [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `CopyAssignable`. |
| 377 | * [*Effects:] |
| 378 | [table |
| 379 | [] |
| 380 | [[][[*`*this` contains a value]][[*`*this` does not contain a value]]] |
| 381 | [[[*`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`]] |
| 382 | [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]] |
| 383 | ] |
| 384 | * [*Returns:] `*this`; |
| 385 | * [*Postconditions:] `bool(rhs) == bool(*this)`. |
| 386 | * [*Exception Safety:] If any exception is thrown, the initialization state of `*this` and `rhs` remains unchanged. |
| 387 | If an exception is thrown during the call to `T`'s copy constructor, no effect. |
| 388 | 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. |
| 389 | * [*Example:] |
| 390 | `` |
| 391 | T v; |
| 392 | optional<T> opt(v); |
| 393 | optional<T> def ; |
| 394 | |
| 395 | opt = def ; |
| 396 | assert ( !def ) ; |
| 397 | // previous value (copy of 'v') destroyed from within 'opt'. |
| 398 | `` |
| 399 | |
| 400 | |
| 401 | __SPACE__ |
| 402 | |
| 403 | [#reference_optional_operator_move_equal_optional] |
| 404 | |
| 405 | [: `optional& optional<T>::operator= ( optional&& rhs ) noexcept(`['see below]`);`] |
| 406 | |
| 407 | * [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `MoveAssignable`. |
| 408 | * [*Effects:] |
| 409 | [table |
| 410 | [] |
| 411 | [[][[*`*this` contains a value]][[*`*this` does not contain a value]]] |
| 412 | [[[*`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)`]] |
| 413 | [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]] |
| 414 | ] |
| 415 | * [*Returns:] `*this`; |
| 416 | * [*Postconditions:] `bool(rhs) == bool(*this)`. |
| 417 | * [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value`. |
| 418 | * [*Exception Safety:] If any exception is thrown, the initialization state of `*this` and `rhs` remains unchanged. If an exception is |
| 419 | thrown during the call to `T`'s move constructor, the state of `*rhs` is determined by the exception safety guarantee |
| 420 | 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. |
| 421 | * [*Example:] |
| 422 | `` |
| 423 | optional<T> opt(T(2)) ; |
| 424 | optional<T> def ; |
| 425 | |
| 426 | opt = def ; |
| 427 | assert ( def ) ; |
| 428 | assert ( opt ) ; |
| 429 | assert ( *opt == T(2) ) ; |
| 430 | `` |
| 431 | |
| 432 | |
| 433 | __SPACE__ |
| 434 | |
| 435 | |
| 436 | [#reference_optional_operator_equal_other_optional] |
| 437 | |
| 438 | [: `template<U> optional& optional<T>::operator= ( optional<U> const& rhs ) ;`] |
| 439 | |
| 440 | * [*Effect:] |
| 441 | [table |
| 442 | [] |
| 443 | [[][[*`*this` contains a value]][[*`*this` does not contain a value]]] |
| 444 | [[[*`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`]] |
| 445 | [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]] |
| 446 | ] |
| 447 | * [*Returns:] `*this`. |
| 448 | * [*Postconditions:] `bool(rhs) == bool(*this)`. |
| 449 | * [*Exception Safety:] If any exception is thrown, the result of the expression `bool(*this)` remains unchanged. |
| 450 | If an exception is thrown during the call to `T`'s constructor, no effect. |
| 451 | 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. |
| 452 | * [*Example:] |
| 453 | `` |
| 454 | T v; |
| 455 | optional<T> opt0(v); |
| 456 | optional<U> opt1; |
| 457 | |
| 458 | opt1 = opt0 ; |
| 459 | assert ( *opt1 == static_cast<U>(v) ) ; |
| 460 | `` |
| 461 | |
| 462 | __SPACE__ |
| 463 | |
| 464 | [#reference_optional_operator_move_equal_other_optional] |
| 465 | |
| 466 | [: `template<U> optional& optional<T>::operator= ( optional<U>&& rhs ) ;`] |
| 467 | |
| 468 | * [*Effect:] |
| 469 | [table |
| 470 | [] |
| 471 | [[][[*`*this` contains a value]][[*`*this` does not contain a value]]] |
| 472 | [[[*`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)`]] |
| 473 | [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]] |
| 474 | ] |
| 475 | * [*Returns:] `*this`. |
| 476 | * [*Postconditions:] `bool(rhs) == bool(*this)`. |
| 477 | * [*Exception Safety:] If any exception is thrown, the result of the expression `bool(*this)` remains unchanged. |
| 478 | If an exception is thrown during the call to `T`'s constructor, no effect. |
| 479 | 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. |
| 480 | * [*Example:] |
| 481 | `` |
| 482 | T v; |
| 483 | optional<T> opt0(v); |
| 484 | optional<U> opt1; |
| 485 | |
| 486 | opt1 = std::move(opt0) ; |
| 487 | assert ( opt0 ); |
| 488 | assert ( opt1 ) |
| 489 | assert ( *opt1 == static_cast<U>(v) ) ; |
| 490 | `` |
| 491 | |
| 492 | __SPACE__ |
| 493 | |
| 494 | [#reference_optional_emplace] |
| 495 | |
| 496 | [: `template<class... Args> void optional<T>::emplace( Args&&... args );`] |
| 497 | |
| 498 | * [*Requires:] The compiler supports rvalue references and variadic templates. |
| 499 | * [*Effect:] If `*this` is initialized calls `*this = none`. |
| 500 | Then initializes in-place the contained value as if direct-initializing an object |
| 501 | of type `T` with `std::forward<Args>(args)...`. |
| 502 | * [*Postconditions: ] `*this` is [_initialized]. |
| 503 | * [*Throws:] Whatever the selected `T`'s constructor throws. |
| 504 | * [*Exception Safety:] If an exception is thrown during the initialization of `T`, `*this` is ['uninitialized]. |
| 505 | * [*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]. |
| 506 | * [*Example:] |
| 507 | `` |
| 508 | T v; |
| 509 | optional<const T> opt; |
| 510 | opt.emplace(0); // create in-place using ctor T(int) |
| 511 | opt.emplace(); // destroy previous and default-construct another T |
| 512 | opt.emplace(v); // destroy and copy-construct in-place (no assignment called) |
| 513 | `` |
| 514 | |
| 515 | __SPACE__ |
| 516 | |
| 517 | [#reference_optional_operator_equal_factory] |
| 518 | |
| 519 | [: `template<InPlaceFactory> optional<T>& optional<T>::operator=( InPlaceFactory const& f );`] |
| 520 | [: `template<TypedInPlaceFactory> optional<T>& optional<T>::operator=( TypedInPlaceFactory const& f );`] |
| 521 | |
| 522 | * [*Effect:] Assigns an `optional` with a value of `T` obtained from the |
| 523 | factory. |
| 524 | * [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given] |
| 525 | from the factory `f` (i.e., the value [_is not copied]). |
| 526 | * [*Throws:] Whatever the `T` constructor called by the factory throws. |
| 527 | * [*Notes:] See [link boost_optional.tutorial.in_place_factories In-Place Factories] |
| 528 | * [*Exception Safety:] Exceptions can only be thrown during the call to |
| 529 | the `T` constructor used by the factory; in that case, the `optional` object |
| 530 | will be reset to be ['uninitialized]. |
| 531 | |
| 532 | __SPACE__ |
| 533 | |
| 534 | [#reference_optional_reset_value] |
| 535 | |
| 536 | [: `void optional<T>::reset( T const& v ) ;`] |
| 537 | * [*Deprecated:] same as `operator= ( T const& v) ;` |
| 538 | |
| 539 | __SPACE__ |
| 540 | |
| 541 | [#reference_optional_reset] |
| 542 | |
| 543 | [: `void optional<T>::reset() noexcept ;`] |
| 544 | * [*Deprecated:] Same as `operator=( none_t );` |
| 545 | |
| 546 | __SPACE__ |
| 547 | |
| 548 | [#reference_optional_get] |
| 549 | |
| 550 | [: `T const& optional<T>::get() const ;`] |
| 551 | [: `T& optional<T>::get() ;`] |
| 552 | |
| 553 | [: `inline T const& get ( optional<T> const& ) ;`] |
| 554 | [: `inline T& get ( optional<T> &) ;`] |
| 555 | |
| 556 | * [*Requires:] `*this` is initialized |
| 557 | * [*Returns:] A reference to the contained value |
| 558 | * [*Throws:] Nothing. |
| 559 | * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`. |
| 560 | |
| 561 | |
| 562 | __SPACE__ |
| 563 | |
| 564 | [#reference_optional_operator_asterisk] |
| 565 | |
| 566 | [: `T const& optional<T>::operator*() const& ;`] |
| 567 | [: `T& optional<T>::operator*() &;`] |
| 568 | |
| 569 | * [*Requires:] `*this` is initialized |
| 570 | * [*Returns:] A reference to the contained value |
| 571 | * [*Throws:] Nothing. |
| 572 | * [*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. |
| 573 | * [*Example:] |
| 574 | `` |
| 575 | T v ; |
| 576 | optional<T> opt ( v ); |
| 577 | T const& u = *opt; |
| 578 | assert ( u == v ) ; |
| 579 | T w ; |
| 580 | *opt = w ; |
| 581 | assert ( *opt == w ) ; |
| 582 | `` |
| 583 | |
| 584 | __SPACE__ |
| 585 | |
| 586 | [#reference_optional_operator_asterisk_move] |
| 587 | |
| 588 | [: `T&& optional<T>::operator*() &&;`] |
| 589 | |
| 590 | * [*Requires:] `*this` contains a value. |
| 591 | * [*Effects:] Equivalent to `return std::move(*val);`. |
| 592 | * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`. On compilers that do not support ref-qualifiers on member functions this overload is not present. |
| 593 | |
| 594 | |
| 595 | __SPACE__ |
| 596 | |
| 597 | [#reference_optional_value] |
| 598 | |
| 599 | [: `T const& optional<T>::value() const& ;`] |
| 600 | [: `T& optional<T>::value() & ;`] |
| 601 | |
| 602 | * [*Effects:] Equivalent to `return bool(*this) ? *val : throw bad_optional_access();`. |
| 603 | * [*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. |
| 604 | * [*Example:] |
| 605 | `` |
| 606 | T v ; |
| 607 | optional<T> o0, o1 ( v ); |
| 608 | assert ( o1.value() == v ); |
| 609 | |
| 610 | try { |
| 611 | o0.value(); // throws |
| 612 | assert ( false ); |
| 613 | } |
| 614 | catch(bad_optional_access&) { |
| 615 | assert ( true ); |
| 616 | } |
| 617 | `` |
| 618 | |
| 619 | __SPACE__ |
| 620 | |
| 621 | [#reference_optional_value_move] |
| 622 | |
| 623 | [: `T&& optional<T>::value() && ;`] |
| 624 | |
| 625 | * [*Effects:] Equivalent to `return bool(*this) ? std::move(*val) : throw bad_optional_access();`. |
| 626 | * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present. |
| 627 | |
| 628 | __SPACE__ |
| 629 | |
| 630 | |
| 631 | [#reference_optional_value_or] |
| 632 | |
| 633 | [: `template<class U> T optional<T>::value_or(U && v) const& ;`] |
| 634 | |
| 635 | * [*Effects:] Equivalent to `if (*this) return **this; else return std::forward<U>(v);`. |
| 636 | * [*Remarks:] If `T` is not __COPY_CONSTRUCTIBLE__ or `U &&` is not convertible to `T`, the program is ill-formed. |
| 637 | * [*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&`. |
| 638 | |
| 639 | __SPACE__ |
| 640 | |
| 641 | [#reference_optional_value_or_move] |
| 642 | |
| 643 | [: `template<class U> T optional<T>::value_or(U && v) && ;`] |
| 644 | |
| 645 | * [*Effects:] Equivalent to `if (*this) return std::move(**this); else return std::forward<U>(v);`. |
| 646 | * [*Remarks:] If `T` is not __MOVE_CONSTRUCTIBLE__ or `U &&` is not convertible to `T`, the program is ill-formed. |
| 647 | * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present. |
| 648 | |
| 649 | __SPACE__ |
| 650 | |
| 651 | [#reference_optional_value_or_call] |
| 652 | |
| 653 | [: `template<class F> T optional<T>::value_or_eval(F f) const& ;`] |
| 654 | |
| 655 | * [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`. |
| 656 | * [*Effects:] `if (*this) return **this; else return f();`. |
| 657 | * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function. |
| 658 | * [*Example:] |
| 659 | `` |
| 660 | int complain_and_0() |
| 661 | { |
| 662 | clog << "no value returned, using default" << endl; |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | optional<int> o1 = 1; |
| 667 | optional<int> oN = none; |
| 668 | |
| 669 | int i = o1.value_or_eval(complain_and_0); // fun not called |
| 670 | assert (i == 1); |
| 671 | |
| 672 | int j = oN.value_or_eval(complain_and_0); // fun called |
| 673 | assert (i == 0); |
| 674 | `` |
| 675 | |
| 676 | __SPACE__ |
| 677 | |
| 678 | [#reference_optional_value_or_call_move] |
| 679 | |
| 680 | [: `template<class F> T optional<T>::value_or_eval(F f) && ;`] |
| 681 | |
| 682 | * [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`. |
| 683 | * [*Effects:] `if (*this) return std::move(**this); else return f();`. |
| 684 | * [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present. |
| 685 | |
| 686 | __SPACE__ |
| 687 | |
| 688 | [#reference_optional_map] |
| 689 | |
| 690 | [: `template<class F> auto optional<T>::map(F f) const& -> `['see below]` ;`] |
| 691 | [: `template<class F> auto optional<T>::map(F f) & -> `['see below]` ;`] |
| 692 | |
| 693 | * [*Effects:] `if (*this) return f(**this); else return none;` |
| 694 | * [*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. |
| 695 | * [*Example:] |
| 696 | `` |
| 697 | auto length = [](const string& s){ return s.size(); }; |
| 698 | optional<string> o1 {}, o2 {"cat"}; |
| 699 | optional<size_t> os1 = o1.map(length), os2 = o2.map(length); |
| 700 | assert ( !os1 ) ; |
| 701 | assert ( os2 ) ; |
| 702 | assert ( *os2 == 3 ) ; |
| 703 | `` |
| 704 | |
| 705 | __SPACE__ |
| 706 | |
| 707 | [#reference_optional_map_move] |
| 708 | |
| 709 | [: `template<class F> auto optional<T>::map(F f) && -> `['see below]` ;`] |
| 710 | |
| 711 | * [*Effects:] `if (*this) return f(std::move(**this)); else return none;` |
| 712 | * [*Notes:] The return type of this overload is `optional<decltype(f(istd::move(**this)))>`. |
| 713 | |
| 714 | __SPACE__ |
| 715 | |
| 716 | [#reference_optional_flat_map] |
| 717 | |
| 718 | [: `template<class F> auto optional<T>::flat_map(F f) const& -> `['see below]` ;`] |
| 719 | [: `template<class F> auto optional<T>::flat_map(F f) & -> `['see below]` ;`] |
| 720 | |
| 721 | * [*Requires:] The return type of expression `f(**this)` is `optional<U>` for some object or reference type `U`. |
| 722 | * [*Effects:] `if (*this) return f(**this); else return none;` |
| 723 | * [*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. |
| 724 | * [*Example:] |
| 725 | `` |
| 726 | optional<char> first_char(const string& s) { |
| 727 | return s.empty() ? none : optional<char>(s[0]); |
| 728 | }; |
| 729 | optional<string> o1 {}, o2 {"cat"}; |
| 730 | optional<char> os1 = o1.flat_map(first_char), os2 = o2.flat_map(first_char); |
| 731 | assert ( !os1 ) ; |
| 732 | assert ( os2 ) ; |
| 733 | assert ( *os2 == 'c' ) ; |
| 734 | `` |
| 735 | __SPACE__ |
| 736 | |
| 737 | [#reference_optional_flat_map_move] |
| 738 | |
| 739 | [: `template<class F> auto optional<T>::flat_map(F f) && -> `['see below]` ;`] |
| 740 | |
| 741 | * [*Requires:] The return type of expression `f(std::move(**this))` is `optional<U>` for some object or reference type `U`. |
| 742 | * [*Effects:] `if (*this) return f(std::move(**this)); else return none;` |
| 743 | * [*Notes:] The return type of this overload is `optional<U>`. |
| 744 | |
| 745 | __SPACE__ |
| 746 | |
| 747 | [#reference_optional_get_value_or_value] |
| 748 | |
| 749 | [: `T const& optional<T>::get_value_or( T const& default) const ;`] |
| 750 | [: `T& optional<T>::get_value_or( T& default ) ;`] |
| 751 | |
| 752 | * [*Deprecated:] Use `value_or()` instead. |
| 753 | * [*Returns:] A reference to the contained value, if any, or `default`. |
| 754 | * [*Throws:] Nothing. |
| 755 | * [*Example:] |
| 756 | `` |
| 757 | T v, z ; |
| 758 | optional<T> def; |
| 759 | T const& y = def.get_value_or(z); |
| 760 | assert ( y == z ) ; |
| 761 | |
| 762 | optional<T> opt ( v ); |
| 763 | T const& u = opt.get_value_or(z); |
| 764 | assert ( u == v ) ; |
| 765 | assert ( u != z ) ; |
| 766 | `` |
| 767 | |
| 768 | |
| 769 | __SPACE__ |
| 770 | |
| 771 | [#reference_optional_get_ptr] |
| 772 | |
| 773 | [: `T const* optional<T>::get_ptr() const ;`] |
| 774 | [: `T* optional<T>::get_ptr() ;`] |
| 775 | |
| 776 | * [*Returns:] If `*this` is initialized, a pointer to the contained value; |
| 777 | else `0` (['null]). |
| 778 | * [*Throws:] Nothing. |
| 779 | * [*Notes:] The contained value is permanently stored within `*this`, so you |
| 780 | should not hold nor delete this pointer |
| 781 | * [*Example:] |
| 782 | `` |
| 783 | T v; |
| 784 | optional<T> opt(v); |
| 785 | optional<T> const copt(v); |
| 786 | T* p = opt.get_ptr() ; |
| 787 | T const* cp = copt.get_ptr(); |
| 788 | assert ( p == get_pointer(opt) ); |
| 789 | assert ( cp == get_pointer(copt) ) ; |
| 790 | `` |
| 791 | |
| 792 | __SPACE__ |
| 793 | |
| 794 | [#reference_optional_operator_arrow] |
| 795 | |
| 796 | [: `T const* optional<T>::operator ->() const ;`] |
| 797 | [: `T* optional<T>::operator ->() ;`] |
| 798 | |
| 799 | * [*Requires: ] `*this` is initialized. |
| 800 | * [*Returns:] A pointer to the contained value. |
| 801 | * [*Throws:] Nothing. |
| 802 | * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`. |
| 803 | * [*Example:] |
| 804 | `` |
| 805 | struct X { int mdata ; } ; |
| 806 | X x ; |
| 807 | optional<X> opt (x); |
| 808 | opt->mdata = 2 ; |
| 809 | `` |
| 810 | |
| 811 | __SPACE__ |
| 812 | |
| 813 | [#reference_optional_operator_bool] |
| 814 | |
| 815 | [: `explicit optional<T>::operator bool() const noexcept ;`] |
| 816 | [: `bool optional<T>::has_value() const noexcept ;`] |
| 817 | |
| 818 | * [*Returns:] `get_ptr() != 0`. |
| 819 | * [*Notes:] On compilers that do not support explicit conversion operators this falls back to safe-bool idiom. |
| 820 | * [*Example:] |
| 821 | `` |
| 822 | optional<T> def ; |
| 823 | assert ( def == 0 ); |
| 824 | optional<T> opt ( v ) ; |
| 825 | assert ( opt ); |
| 826 | assert ( opt != 0 ); |
| 827 | `` |
| 828 | |
| 829 | __SPACE__ |
| 830 | |
| 831 | [#reference_optional_operator_not] |
| 832 | |
| 833 | [: `bool optional<T>::operator!() noexcept ;`] |
| 834 | |
| 835 | * [*Returns:] If `*this` is uninitialized, `true`; else `false`. |
| 836 | * [*Notes:] This operator is provided for those compilers which can't |
| 837 | use the ['unspecified-bool-type operator] in certain boolean contexts. |
| 838 | * [*Example:] |
| 839 | `` |
| 840 | optional<T> opt ; |
| 841 | assert ( !opt ); |
| 842 | *opt = some_T ; |
| 843 | |
| 844 | // Notice the "double-bang" idiom here. |
| 845 | assert ( !!opt ) ; |
| 846 | `` |
| 847 | |
| 848 | __SPACE__ |
| 849 | |
| 850 | [#reference_optional_is_initialized] |
| 851 | |
| 852 | [: `bool optional<T>::is_initialized() const ;`] |
| 853 | |
| 854 | * [*Deprecated:] Same as `explicit operator bool () ;` |
| 855 | |
| 856 | |
| 857 | [endsect] |
| 858 | |
| 859 | [section Detailed Semantics - Optional References] |
| 860 | |
| 861 | __SPACE__ |
| 862 | |
| 863 | [#reference_optional_ref_default_ctor] |
| 864 | |
| 865 | [: `optional<T&>::optional() noexcept;`] |
| 866 | [: `optional<T&>::optional(none_t) noexcept;`] |
| 867 | |
| 868 | * [*Postconditions:] `bool(*this) == false`; `*this` refers to nothing. |
| 869 | |
| 870 | |
| 871 | __SPACE__ |
| 872 | |
| 873 | [#reference_optional_ref_value_ctor] |
| 874 | |
| 875 | [: `template<class R> optional<T&>::optional(R&& r) noexcept;`] |
| 876 | * [*Postconditions:] `bool(*this) == true`; `addressof(**this) == addressof(r)`. |
| 877 | * [*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`. |
| 878 | * [*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]. |
| 879 | * [*Example:] |
| 880 | `` |
| 881 | T v; |
| 882 | T& vref = v ; |
| 883 | optional<T&> opt(vref); |
| 884 | assert ( *opt == v ) ; |
| 885 | ++ v ; // mutate referee |
| 886 | assert (*opt == v); |
| 887 | `` |
| 888 | |
| 889 | __SPACE__ |
| 890 | |
| 891 | [#reference_optional_ref_cond_value_ctor] |
| 892 | |
| 893 | [: `template<class R> optional<T&>::optional(bool cond, R&& r) noexcept;`] |
| 894 | * [*Effects: ] Initializes `ref` with expression `cond ? addressof(r) : nullptr`. |
| 895 | * [*Postconditions:] `bool(*this) == cond`; If `bool(*this)`, `addressof(**this) == addressof(r)`. |
| 896 | * [*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`. |
| 897 | |
| 898 | __SPACE__ |
| 899 | |
| 900 | [#reference_optional_ref_copy_ctor] |
| 901 | |
| 902 | [: `optional<T&>::optional ( optional const& rhs ) noexcept ;`] |
| 903 | |
| 904 | * [*Effects: ] Initializes `ref` with expression `rhs.ref`. |
| 905 | |
| 906 | * [*Postconditions:] `bool(*this) == bool(rhs)`. |
| 907 | |
| 908 | * [*Example:] |
| 909 | `` |
| 910 | optional<T&> uninit ; |
| 911 | assert (!uninit); |
| 912 | |
| 913 | optional<T&> uinit2 ( uninit ) ; |
| 914 | assert ( uninit2 == uninit ); |
| 915 | |
| 916 | T v = 2 ; T& ref = v ; |
| 917 | optional<T> init(ref); |
| 918 | assert ( *init == v ) ; |
| 919 | |
| 920 | optional<T> init2 ( init ) ; |
| 921 | assert ( *init2 == v ) ; |
| 922 | |
| 923 | v = 3 ; |
| 924 | |
| 925 | assert ( *init == 3 ) ; |
| 926 | assert ( *init2 == 3 ) ; |
| 927 | `` |
| 928 | |
| 929 | __SPACE__ |
| 930 | |
| 931 | [#reference_optional_ref_ctor_from_opt_U] |
| 932 | |
| 933 | [: `template<class U> explicit optional<T&>::optional ( optional<U&> const& rhs ) noexcept ;`] |
| 934 | |
| 935 | * [*Requires:] `is_convertible<U&, T&>::value` is `true`. |
| 936 | |
| 937 | * [*Effects: ] Initializes `ref` with expression `rhs.ref`. |
| 938 | |
| 939 | * [*Postconditions:] `bool(*this) == bool(rhs)`. |
| 940 | |
| 941 | |
| 942 | __SPACE__ |
| 943 | |
| 944 | [#reference_optional_ref_assign_none_t] |
| 945 | |
| 946 | [: `optional<T&>::operator= ( none_t ) noexcept ;`] |
| 947 | |
| 948 | * [*Effects: ] Assigns `ref` with expression `nullptr`. |
| 949 | |
| 950 | * [*returns:] `*this`. |
| 951 | |
| 952 | * [*Postconditions:] `bool(*this) == false`. |
| 953 | |
| 954 | |
| 955 | |
| 956 | [#reference_optional_ref_copy_assign] |
| 957 | |
| 958 | [: `optional& optional<T&>::operator= ( optional const& rhs ) noexcept ;`] |
| 959 | |
| 960 | * [*Effects: ] Assigns `ref` with expression `rhs.ref`. |
| 961 | |
| 962 | * [*returns:] `*this`. |
| 963 | |
| 964 | * [*Postconditions:] `bool(*this) == bool(rhs)`. |
| 965 | |
| 966 | * [*Notes:] This behaviour is called ['rebinding semantics]. See [link boost_optional.tutorial.optional_references.rebinding_semantics_for_assignment_of_optional_references here] for details. |
| 967 | |
| 968 | * [*Example:] |
| 969 | `` |
| 970 | int a = 1 ; |
| 971 | int b = 2 ; |
| 972 | T& ra = a ; |
| 973 | T& rb = b ; |
| 974 | optional<int&> def ; |
| 975 | optional<int&> ora(ra) ; |
| 976 | optional<int&> orb(rb) ; |
| 977 | |
| 978 | def = orb ; // binds 'def' to 'b' through 'rb' wrapped within 'orb' |
| 979 | assert ( *def == b ) ; |
| 980 | *def = ora ; // changes the value of 'b' to a copy of the value of 'a' |
| 981 | assert ( b == a ) ; |
| 982 | int c = 3; |
| 983 | int& rc = c ; |
| 984 | optional<int&> orc(rc) ; |
| 985 | ora = orc ; // REBINDS ora to 'c' through 'rc' |
| 986 | c = 4 ; |
| 987 | assert ( *ora == 4 ) ; |
| 988 | `` |
| 989 | |
| 990 | |
| 991 | [#reference_optional_ref_assign_optional_U] |
| 992 | |
| 993 | [: `template<class U> optional& optional<T&>::operator= ( optional<U&> const& rhs ) noexcept ;`] |
| 994 | |
| 995 | * [*Requires:] `is_convertible<U&, T&>::value` is `true`. |
| 996 | |
| 997 | * [*Effects: ] Assigns `ref` with expression `rhs.ref`. |
| 998 | |
| 999 | * [*returns:] `*this`. |
| 1000 | |
| 1001 | * [*Postconditions:] `bool(*this) == bool(rhs)`. |
| 1002 | |
| 1003 | |
| 1004 | __SPACE__ |
| 1005 | |
| 1006 | [#reference_optional_ref_assign_R] |
| 1007 | |
| 1008 | [: `template<class R> optional& optional<T&>::operator= ( R&& r ) noexcept ;`] |
| 1009 | |
| 1010 | * [*Effects: ] Assigns `ref` with expression `r`. |
| 1011 | |
| 1012 | * [*returns:] `*this`. |
| 1013 | |
| 1014 | * [*Postconditions:] `bool(*this) == true`. |
| 1015 | |
| 1016 | * [*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`. |
| 1017 | |
| 1018 | * [*Example:] |
| 1019 | `` |
| 1020 | int a = 1 ; |
| 1021 | int b = 2 ; |
| 1022 | T& ra = a ; |
| 1023 | T& rb = b ; |
| 1024 | optional<int&> def ; |
| 1025 | optional<int&> opt(ra) ; |
| 1026 | |
| 1027 | def = rb ; // binds 'def' to 'b' through 'rb' |
| 1028 | assert ( *def == b ) ; |
| 1029 | *def = a ; // changes the value of 'b' to a copy of the value of 'a' |
| 1030 | assert ( b == a ) ; |
| 1031 | int c = 3; |
| 1032 | int& rc = c ; |
| 1033 | opt = rc ; // REBINDS to 'c' through 'rc' |
| 1034 | c = 4 ; |
| 1035 | assert ( *opt == 4 ) ; |
| 1036 | `` |
| 1037 | |
| 1038 | __SPACE__ |
| 1039 | |
| 1040 | [#reference_optional_ref_emplace_R] |
| 1041 | |
| 1042 | [: `void optional<T&>::emplace( R&& r ) noexcept ;`] |
| 1043 | * [*Effects: ] Assigns `ref` with expression `r`. |
| 1044 | * [*Postconditions:] `bool(*this) == true`. |
| 1045 | * [*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`. |
| 1046 | |
| 1047 | __SPACE__ |
| 1048 | |
| 1049 | [#reference_optional_ref_get] |
| 1050 | [: `T& optional<T&>::get() const ;`] |
| 1051 | [: `T& optional<T&>::operator *() const ;`] |
| 1052 | * [*Requires:] `bool(*this) == true`. |
| 1053 | * [*Effects: ] Returns `*ref`. |
| 1054 | * [*Throws: ] Nothing. |
| 1055 | * [*Example:] |
| 1056 | `` |
| 1057 | T v ; |
| 1058 | T& vref = v ; |
| 1059 | optional<T&> opt ( vref ); |
| 1060 | T const& vref2 = *opt; |
| 1061 | assert ( vref2 == v ) ; |
| 1062 | ++ v ; |
| 1063 | assert ( *opt == v ) ; |
| 1064 | `` |
| 1065 | |
| 1066 | __SPACE__ |
| 1067 | |
| 1068 | [#reference_optional_ref_arrow] |
| 1069 | [: `T* optional<T&>::operator -> () const ;`] |
| 1070 | * [*Requires:] `bool(*this) == true`. |
| 1071 | * [*Effects: ] Returns `ref`. |
| 1072 | * [*Throws: ] Nothing. |
| 1073 | |
| 1074 | __SPACE__ |
| 1075 | |
| 1076 | [#reference_optional_ref_value] |
| 1077 | [: `T& optional<T&>::value() const ;`] |
| 1078 | * [*Effects:] Equivalent to `return bool(*this) ? *val : throw bad_optional_access();`. |
| 1079 | |
| 1080 | __SPACE__ |
| 1081 | |
| 1082 | [#reference_optional_ref_value_or] |
| 1083 | [: `template<class R> T& optional<T&>::value_or( R&& r ) const noexcept;`] |
| 1084 | * [*Effects:] Equivalent to `if (*this) return **this; else return r;`. |
| 1085 | * [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. |
| 1086 | |
| 1087 | __SPACE__ |
| 1088 | |
| 1089 | [#reference_optional_ref_value_or_eval] |
| 1090 | [: `template<class F> T& optional<T&>::value_or( F f ) const ;`] |
| 1091 | * [*Effects:] Equivalent to `if (*this) return **this; else return f();`. |
| 1092 | * [*Remarks:] Unless `decltype(f())` is an lvalue reference, the program is ill-formed. |
| 1093 | |
| 1094 | __SPACE__ |
| 1095 | |
| 1096 | [#reference_optional_ref_map] |
| 1097 | [: `template<class F> auto optional<T&>::map( F f ) const -> `['see below]`;`] |
| 1098 | * [*Effects:] Equivalent to `if (*this) return f(**this); else return none;`. |
| 1099 | * [*Remarks:] The return type of this function is `optional<decltype(f(**this))>`. |
| 1100 | |
| 1101 | __SPACE__ |
| 1102 | |
| 1103 | [#reference_optional_ref_flat_map] |
| 1104 | [: `template<class F> auto optional<T&>::flat_map( F f ) const -> `['see below]`;`] |
| 1105 | * [*Requires:] The return type of expression `f(**this)` is `optional<U>` for some object or reference type `U`. |
| 1106 | * [*Effects:] Equivalent to `if (*this) return f(**this); else return none;`. |
| 1107 | * [*Remarks:] The return type of this function is `optional<U>`. |
| 1108 | |
| 1109 | __SPACE__ |
| 1110 | |
| 1111 | [#reference_optional_ref_get_ptr] |
| 1112 | [: `T* optional<T&>::get_ptr () const noexcept;`] |
| 1113 | * [*Returns:] `ref`. |
| 1114 | |
| 1115 | __SPACE__ |
| 1116 | |
| 1117 | [#reference_optional_ref_operator_bool] |
| 1118 | [: `bool has_value() const noexcept;`] |
| 1119 | [: `optional<T&>::operator bool () const noexcept;`] |
| 1120 | * [*Returns:] `bool(ref)`. |
| 1121 | |
| 1122 | __SPACE__ |
| 1123 | |
| 1124 | [#reference_optional_ref_operator_not] |
| 1125 | [: `optional<T&>::operator ! () const noexcept;`] |
| 1126 | * [*Returns:] `!bool(ref)`. |
| 1127 | |
| 1128 | __SPACE__ |
| 1129 | |
| 1130 | [#reference_optional_ref_reset] |
| 1131 | [: `void optional<T&>::reset() noexcept;`] |
| 1132 | * [*Effects:] Use `*this = none` instead. |
| 1133 | * [*Remarks:] This function is depprecated. |
| 1134 | |
| 1135 | __SPACE__ |
| 1136 | |
| 1137 | [#reference_optional_ref_reset_value] |
| 1138 | [: `template<class R> void optional<T&>::reset ( R&& r) noexcept;`] |
| 1139 | * [*Effects:] Equivalent to `*this = std::forward<R>(r)`. |
| 1140 | * [*Remarks:] This function is depprecated. |
| 1141 | |
| 1142 | __SPACE__ |
| 1143 | |
| 1144 | [#reference_optional_ref_is_initialized] |
| 1145 | [: `bool optional<T&>::is_initialized() const noexcept;`] |
| 1146 | * [*Effects:] Equivalent to `return bool(*this)`. |
| 1147 | * [*Remarks:] This function is depprecated. |
| 1148 | |
| 1149 | __SPACE__ |
| 1150 | |
| 1151 | [#reference_optional_ref_get_value_or_value] |
| 1152 | [: `template<class R> T& optional<T&>::get_value_or( R&& r ) const noexcept;`] |
| 1153 | * [*Effects:] Equivalent to `return value_or(std::forward<R>(r);`. |
| 1154 | * [*Remarks:] This function is depprecated. |
| 1155 | |
| 1156 | [endsect] |
| 1157 | |
| 1158 | |
| 1159 | [section Detailed Semantics - Free Functions] |
| 1160 | |
| 1161 | |
| 1162 | __SPACE__ |
| 1163 | |
| 1164 | [#reference_make_optional_value] |
| 1165 | |
| 1166 | [: `optional<T> make_optional( T const& v )`] |
| 1167 | |
| 1168 | * [*Returns: ] `optional<T>(v)` for the ['deduced] type `T` of `v`. |
| 1169 | * [*Example:] |
| 1170 | `` |
| 1171 | template<class T> void foo ( optional<T> const& opt ) ; |
| 1172 | |
| 1173 | foo ( make_optional(1+1) ) ; // Creates an optional<int> |
| 1174 | `` |
| 1175 | |
| 1176 | __SPACE__ |
| 1177 | |
| 1178 | [#reference_make_optional_rvalue] |
| 1179 | |
| 1180 | [: `optional<std::decay_t<T>> make_optional( T && v )`] |
| 1181 | |
| 1182 | * [*Returns: ] `optional<std::decay_t<T>>(std::move(v))` for the ['deduced] type `T` of `v`. |
| 1183 | |
| 1184 | |
| 1185 | __SPACE__ |
| 1186 | |
| 1187 | [#reference_make_optional_bool_value] |
| 1188 | |
| 1189 | [: `optional<T> make_optional( bool condition, T const& v )`] |
| 1190 | |
| 1191 | * [*Returns: ] `optional<T>(condition, v)` for the ['deduced] type `T` of `v`. |
| 1192 | * [*Example:] |
| 1193 | `` |
| 1194 | optional<double> calculate_foo() |
| 1195 | { |
| 1196 | double val = compute_foo(); |
| 1197 | return make_optional(is_not_nan_and_finite(val),val); |
| 1198 | } |
| 1199 | |
| 1200 | optional<double> v = calculate_foo(); |
| 1201 | if ( !v ) |
| 1202 | error("foo wasn't computed"); |
| 1203 | `` |
| 1204 | |
| 1205 | __SPACE__ |
| 1206 | |
| 1207 | [#reference_make_optional_bool_rvalue] |
| 1208 | |
| 1209 | [: `optional<std::decay_t<T>> make_optional( bool condition, T && v )`] |
| 1210 | |
| 1211 | * [*Returns: ] `optional<std::decay_t<T>>(condition, std::move(v))` for the ['deduced] type `T` of `v`. |
| 1212 | |
| 1213 | |
| 1214 | __SPACE__ |
| 1215 | |
| 1216 | [#reference_operator_compare_equal_optional_optional] |
| 1217 | |
| 1218 | [: `bool operator == ( optional<T> const& x, optional<T> const& y );`] |
| 1219 | |
| 1220 | * [*Requires:] `T` shall meet requirements of __SGI_EQUALITY_COMPARABLE__. |
| 1221 | * [*Returns:] If both `x` and `y` are initialized, `(*x == *y)`. If only |
| 1222 | `x` or `y` is initialized, `false`. If both are uninitialized, `true`. |
| 1223 | * [*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. |
| 1224 | 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 |
| 1225 | __FUNCTION_EQUAL_POINTEES__ instead |
| 1226 | * [*Example:] |
| 1227 | `` |
| 1228 | optional<T> oN, oN_; |
| 1229 | optional<T> o1(T(1)), o1_(T(1)); |
| 1230 | optional<T> o2(T(2)); |
| 1231 | |
| 1232 | assert ( oN == oN ); // Identity implies equality |
| 1233 | assert ( o1 == o1 ); // |
| 1234 | |
| 1235 | assert ( oN == oN_ ); // Both uninitialized compare equal |
| 1236 | |
| 1237 | assert ( oN != o1 ); // Initialized unequal to initialized. |
| 1238 | |
| 1239 | assert ( o1 == o1_ ); // Both initialized compare as (*lhs == *rhs) |
| 1240 | assert ( o1 != o2 ); // |
| 1241 | `` |
| 1242 | |
| 1243 | __SPACE__ |
| 1244 | |
| 1245 | [#reference_operator_compare_less_optional_optional] |
| 1246 | |
| 1247 | [: `bool operator < ( optional<T> const& x, optional<T> const& y );`] |
| 1248 | |
| 1249 | * [*Requires:] Expression `*x < *y` shall be well-formed and its result shall be convertible to `bool`. |
| 1250 | * [*Returns:] `(!y) ? false : (!x) ? true : *x < *y`. |
| 1251 | * [*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. |
| 1252 | Pointers have shallow relational operators while `optional` has deep relational operators. Do not use `operator<` directly in generic code |
| 1253 | 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__. |
| 1254 | * [*Example:] |
| 1255 | `` |
| 1256 | optional<T> oN, oN_; |
| 1257 | optional<T> o0(T(0)); |
| 1258 | optional<T> o1(T(1)); |
| 1259 | |
| 1260 | assert ( !(oN < oN) ); // Identity implies equivalence |
| 1261 | assert ( !(o1 < o1) ); |
| 1262 | |
| 1263 | assert ( !(oN < oN_) ); // Two uninitialized are equivalent |
| 1264 | assert ( !(oN_ < oN) ); |
| 1265 | |
| 1266 | assert ( oN < o0 ); // Uninitialized is less than initialized |
| 1267 | assert ( !(o0 < oN) ); |
| 1268 | |
| 1269 | assert ( o1 < o2 ) ; // Two initialized compare as (*lhs < *rhs) |
| 1270 | assert ( !(o2 < o1) ) ; |
| 1271 | assert ( !(o2 < o2) ) ; |
| 1272 | `` |
| 1273 | |
| 1274 | __SPACE__ |
| 1275 | |
| 1276 | [#reference_operator_compare_not_equal_optional_optional] |
| 1277 | |
| 1278 | [: `bool operator != ( optional<T> const& x, optional<T> const& y );`] |
| 1279 | |
| 1280 | * [*Returns: ] `!( x == y );` |
| 1281 | |
| 1282 | __SPACE__ |
| 1283 | |
| 1284 | [#reference_operator_compare_greater_optional_optional] |
| 1285 | |
| 1286 | [: `bool operator > ( optional<T> const& x, optional<T> const& y );`] |
| 1287 | |
| 1288 | * [*Returns: ] `( y < x );` |
| 1289 | |
| 1290 | __SPACE__ |
| 1291 | |
| 1292 | [#reference_operator_compare_less_or_equal_optional_optional] |
| 1293 | |
| 1294 | [: `bool operator <= ( optional<T> const& x, optional<T> const& y );`] |
| 1295 | |
| 1296 | * [*Returns: ] `!( y < x );` |
| 1297 | |
| 1298 | __SPACE__ |
| 1299 | |
| 1300 | [#reference_operator_compare_greater_or_equal_optional_optional] |
| 1301 | |
| 1302 | [: `bool operator >= ( optional<T> const& x, optional<T> const& y );`] |
| 1303 | |
| 1304 | * [*Returns: ] `!( x < y );` |
| 1305 | |
| 1306 | __SPACE__ |
| 1307 | |
| 1308 | [#reference_operator_compare_equal_optional_none] |
| 1309 | |
| 1310 | [: `bool operator == ( optional<T> const& x, none_t ) noexcept;`] |
| 1311 | [: `bool operator == ( none_t, optional<T> const& x ) noexcept;`] |
| 1312 | |
| 1313 | * [*Returns:] `!x`. |
| 1314 | * [*Notes:] `T` need not meet requirements of __SGI_EQUALITY_COMPARABLE__. |
| 1315 | |
| 1316 | |
| 1317 | __SPACE__ |
| 1318 | |
| 1319 | [#reference_operator_compare_not_equal_optional_none] |
| 1320 | |
| 1321 | [: `bool operator != ( optional<T> const& x, none_t ) noexcept;`] |
| 1322 | [: `bool operator != ( none_t, optional<T> const& x ) noexcept;`] |
| 1323 | |
| 1324 | * [*Returns: ] `bool(x);` |
| 1325 | |
| 1326 | |
| 1327 | __SPACE__ |
| 1328 | |
| 1329 | |
| 1330 | [#reference_free_get_pointer] |
| 1331 | [: `auto get_pointer ( optional<T>& o ) -> typename optional<T>::pointer_type ;`] |
| 1332 | [: `auto get_pointer ( optional<T> const& o ) -> typename optional<T>::pointer_const_type ;`] |
| 1333 | * [*Returns:] `o.get_ptr()`. |
| 1334 | * [*Throws:] Nothing. |
| 1335 | |
| 1336 | __SPACE__ |
| 1337 | |
| 1338 | |
| 1339 | [#reference_free_get_value_or] |
| 1340 | [: `auto get_optional_value_or ( optional<T>& o, typename optional<T>::reference_type def ) -> typename optional<T>::reference_type ;`] |
| 1341 | [: `auto get_optional_value_or ( optional<T> const& o, typename optional<T>::reference_const_type def ) -> typename optional<T>::reference_const_type ;`] |
| 1342 | * [*Returns:] `o.get_value_or(def)`. |
| 1343 | * [*Throws:] Nothing. |
| 1344 | * [*Remarks:] This function is deprecated. |
| 1345 | |
| 1346 | __SPACE__ |
| 1347 | |
| 1348 | [#reference_swap_optional_optional] |
| 1349 | |
| 1350 | [: `void swap ( optional<T>& x, optional<T>& y ) ;`] |
| 1351 | |
| 1352 | * [*Requires:] Lvalues of type `T` shall be swappable and `T` shall be __MOVE_CONSTRUCTIBLE__. |
| 1353 | * [*Effects:] |
| 1354 | [table |
| 1355 | [] |
| 1356 | [[][[*`*this` contains a value]][[*`*this` does not contain a value]]] |
| 1357 | [[[*`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]] |
| 1358 | [[[*`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]] |
| 1359 | ] |
| 1360 | * [*Postconditions:] The states of `x` and `y` interchanged. |
| 1361 | * [*Throws:] If both are initialized, whatever `swap(T&,T&)` throws. If only |
| 1362 | one is initialized, whatever `T::T ( T&& )` throws. |
| 1363 | * [*Example:] |
| 1364 | `` |
| 1365 | T x(12); |
| 1366 | T y(21); |
| 1367 | optional<T> def0 ; |
| 1368 | optional<T> def1 ; |
| 1369 | optional<T> optX(x); |
| 1370 | optional<T> optY(y); |
| 1371 | |
| 1372 | boost::swap(def0,def1); // no-op |
| 1373 | |
| 1374 | boost::swap(def0,optX); |
| 1375 | assert ( *def0 == x ); |
| 1376 | assert ( !optX ); |
| 1377 | |
| 1378 | boost::swap(def0,optX); // Get back to original values |
| 1379 | |
| 1380 | boost::swap(optX,optY); |
| 1381 | assert ( *optX == y ); |
| 1382 | assert ( *optY == x ); |
| 1383 | `` |
| 1384 | |
| 1385 | __SPACE__ |
| 1386 | |
| 1387 | [#reference_swap_optional_reference] |
| 1388 | [: `void swap ( optional<T&>& x, optional<T&>& y ) noexcept ;`] |
| 1389 | |
| 1390 | * [*Postconditions:] `x` refers to what `y` refererred to before the swap (if anything). `y` refers to whatever `x` referred to before the swap. |
| 1391 | |
| 1392 | * [*Example:] |
| 1393 | `` |
| 1394 | T x(12); |
| 1395 | T y(21); |
| 1396 | |
| 1397 | optional<T&> opt0; |
| 1398 | optional<T&> optX (x); |
| 1399 | optional<T&> optY (y); |
| 1400 | |
| 1401 | boost::swap(optX, optY); |
| 1402 | assert (addressof(*optX) == addressof(y)); |
| 1403 | assert (addressof(*optY) == addressof(x)); |
| 1404 | |
| 1405 | boost::swap(opt0, optX); |
| 1406 | assert ( opt0 ); |
| 1407 | assert ( !optX ); |
| 1408 | assert (addressof(*opt0) == addressof(y)); |
| 1409 | `` |
| 1410 | |
| 1411 | [endsect] |