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 15The following section contains various `assert()` which are used only to show 16the postconditions as sample code. It is not implied that the type `T` must 17support each particular expression but that if the expression is supported, 18the 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`` 33optional<T> def ; 34assert ( !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 47the parameter. 48* [*Example:] 49`` 50#include <boost/none.hpp> 51optional<T> n(none) ; 52assert ( !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] 64of `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`` 71T v; 72optional<T> opt(v); 73assert ( *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`` 92T v1, v2; 93optional<T> opt(std::move(v1)); 94assert ( *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 122its 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`` 129optional<T> uninit ; 130assert (!uninit); 131 132optional<T> uinit2 ( uninit ) ; 133assert ( uninit2 == uninit ); 134 135optional<T> init( T(2) ); 136assert ( *init == T(2) ) ; 137 138optional<T> init2 ( init ) ; 139assert ( 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 152its 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`` 160optional<std::unique_ptr<T>> uninit ; 161assert (!uninit); 162 163optional<std::unique_ptr<T>> uinit2 ( std::move(uninit) ) ; 164assert ( uninit2 == uninit ); 165 166optional<std::unique_ptr<T>> init( std::uniqye_ptr<T>(new T(2)) ); 167assert ( **init == T(2) ) ; 168 169optional<std::unique_ptr<T>> init2 ( std::move(init) ) ; 170assert ( init ); 171assert ( *init == nullptr ); 172assert ( init2 ); 173assert ( **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 185value is a ['copy] of the value of rhs converted to type `T`; else `*this` is 186uninitialized. 187* [*Throws:] Whatever `T::T( U const& )` throws. 188* [*Notes: ] `T::T( U const& )` is called if `rhs` is initialized, which requires a 189valid conversion from `U` to `T`. 190* [*Exception Safety:] Exceptions can only be thrown during `T::T( U const& );` 191in that case, this constructor has no effect. 192* [*Example:] 193`` 194optional<double> x(123.4); 195assert ( *x == 123.4 ) ; 196 197optional<int> y(x) ; 198assert( *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 209value is move-constructed from `*rhs`; else `*this` is 210uninitialized. 211* [*Throws:] Whatever `T::T( U&& )` throws. 212* [*Notes: ] `T::T( U&& )` is called if `rhs` is initialized, which requires a 213valid conversion from `U` to `T`. 214* [*Exception Safety:] Exceptions can only be thrown during `T::T( U&& );` 215in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety guarantee of `T::T( U&& )`. 216* [*Example:] 217`` 218optional<double> x(123.4); 219assert ( *x == 123.4 ) ; 220 221optional<int> y(std::move(x)) ; 222assert( *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 233arguments `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 241optional<std::mutex> om {in_place_init}; 242assert (om); 243 244// creates a unique_lock by calling unique_lock(*om, std::defer_lock) 245optional<std::unique_lock<std::mutex>> ol {in_place_init, *om, std::defer_lock}; 246assert (ol); 247assert (!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`` 264optional<std::vector<std::string>> ov1 {in_place_init_if, false, 3, "A"}; 265assert (!ov1); 266 267optional<std::vector<std::string>> ov2 {in_place_init_if, true, 3, "A"}; 268assert (ov2); 269assert (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 280factory. 281* [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given] 282from 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 286the `T` constructor used by the factory; in that case, this constructor has 287no effect. 288* [*Example:] 289`` 290class C { C ( char, double, std::string ) ; } ; 291 292C v('A',123.4,"hello"); 293 294optional<C> x( in_place ('A', 123.4, "hello") ); // InPlaceFactory used 295optional<C> y( in_place<C>('A', 123.4, "hello") ); // TypedInPlaceFactory used 296 297assert ( *x == v ) ; 298assert ( *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, 320otherwise, its copy-constructor is used. 321* [*Exception Safety:] In the event of an exception, the initialization 322state of `*this` is unchanged and its value unspecified as far as `optional` 323is concerned (it is up to `T`'s `operator=()`). If `*this` is initially 324uninitialized and `T`'s ['copy constructor] fails, `*this` is left properly 325uninitialized. 326* [*Example:] 327`` 328T x; 329optional<T> def ; 330optional<T> opt(x) ; 331 332T y; 333def = y ; 334assert ( *def == y ) ; 335opt = y ; 336assert ( *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, 350otherwise, its move-constructor is used. 351* [*Exception Safety:] In the event of an exception, the initialization 352state of `*this` is unchanged and its value unspecified as far as `optional` 353is concerned (it is up to `T`'s `operator=()`). If `*this` is initially 354uninitialized and `T`'s ['move constructor] fails, `*this` is left properly 355uninitialized. 356* [*Example:] 357`` 358T x; 359optional<T> def ; 360optional<T> opt(x) ; 361 362T y1, y2, yR; 363def = std::move(y1) ; 364assert ( *def == yR ) ; 365opt = std::move(y2) ; 366assert ( *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. 387If an exception is thrown during the call to `T`'s copy constructor, no effect. 388If 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`` 391T v; 392optional<T> opt(v); 393optional<T> def ; 394 395opt = def ; 396assert ( !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 419thrown during the call to `T`'s move constructor, the state of `*rhs` is determined by the exception safety guarantee 420of `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`` 423optional<T> opt(T(2)) ; 424optional<T> def ; 425 426opt = def ; 427assert ( def ) ; 428assert ( opt ) ; 429assert ( *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. 450If an exception is thrown during the call to `T`'s constructor, no effect. 451If 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`` 454T v; 455optional<T> opt0(v); 456optional<U> opt1; 457 458opt1 = opt0 ; 459assert ( *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. 478If an exception is thrown during the call to `T`'s constructor, no effect. 479If 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`` 482T v; 483optional<T> opt0(v); 484optional<U> opt1; 485 486opt1 = std::move(opt0) ; 487assert ( opt0 ); 488assert ( opt1 ) 489assert ( *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`` 508T v; 509optional<const T> opt; 510opt.emplace(0); // create in-place using ctor T(int) 511opt.emplace(); // destroy previous and default-construct another T 512opt.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 523factory. 524* [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given] 525from 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 529the `T` constructor used by the factory; in that case, the `optional` object 530will 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* [*Effects:] 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`` 575T v ; 576optional<T> opt ( v ); 577T const& u = *opt; 578assert ( u == v ) ; 579T w ; 580*opt = w ; 581assert ( *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`` 606T v ; 607optional<T> o0, o1 ( v ); 608assert ( o1.value() == v ); 609 610try { 611 o0.value(); // throws 612 assert ( false ); 613} 614catch(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`` 660int complain_and_0() 661{ 662 clog << "no value returned, using default" << endl; 663 return 0; 664} 665 666optional<int> o1 = 1; 667optional<int> oN = none; 668 669int i = o1.value_or_eval(complain_and_0); // fun not called 670assert (i == 1); 671 672int j = oN.value_or_eval(complain_and_0); // fun called 673assert (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`` 697auto length = [](const string& s){ return s.size(); }; 698optional<string> o1 {}, o2 {"cat"}; 699optional<size_t> os1 = o1.map(length), os2 = o2.map(length); 700assert ( !os1 ) ; 701assert ( os2 ) ; 702assert ( *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`` 726optional<char> first_char(const string& s) { 727 return s.empty() ? none : optional<char>(s[0]); 728}; 729optional<string> o1 {}, o2 {"cat"}; 730optional<char> os1 = o1.flat_map(first_char), os2 = o2.flat_map(first_char); 731assert ( !os1 ) ; 732assert ( os2 ) ; 733assert ( *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`` 757T v, z ; 758optional<T> def; 759T const& y = def.get_value_or(z); 760assert ( y == z ) ; 761 762optional<T> opt ( v ); 763T const& u = opt.get_value_or(z); 764assert ( u == v ) ; 765assert ( 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; 777else `0` (['null]). 778* [*Throws:] Nothing. 779* [*Notes:] The contained value is permanently stored within `*this`, so you 780should not hold nor delete this pointer 781* [*Example:] 782`` 783T v; 784optional<T> opt(v); 785optional<T> const copt(v); 786T* p = opt.get_ptr() ; 787T const* cp = copt.get_ptr(); 788assert ( p == get_pointer(opt) ); 789assert ( 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`` 805struct X { int mdata ; } ; 806X x ; 807optional<X> opt (x); 808opt->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`` 822optional<T> def ; 823assert ( def == 0 ); 824optional<T> opt ( v ) ; 825assert ( opt ); 826assert ( 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 837use the ['unspecified-bool-type operator] in certain boolean contexts. 838* [*Example:] 839`` 840optional<T> opt ; 841assert ( !opt ); 842*opt = some_T ; 843 844// Notice the "double-bang" idiom here. 845assert ( !!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`` 881T v; 882T& vref = v ; 883optional<T&> opt(vref); 884assert ( *opt == v ) ; 885++ v ; // mutate referee 886assert (*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`` 910optional<T&> uninit ; 911assert (!uninit); 912 913optional<T&> uinit2 ( uninit ) ; 914assert ( uninit2 == uninit ); 915 916T v = 2 ; T& ref = v ; 917optional<T> init(ref); 918assert ( *init == v ) ; 919 920optional<T> init2 ( init ) ; 921assert ( *init2 == v ) ; 922 923v = 3 ; 924 925assert ( *init == 3 ) ; 926assert ( *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`` 970int a = 1 ; 971int b = 2 ; 972T& ra = a ; 973T& rb = b ; 974optional<int&> def ; 975optional<int&> ora(ra) ; 976optional<int&> orb(rb) ; 977 978def = orb ; // binds 'def' to 'b' through 'rb' wrapped within 'orb' 979assert ( *def == b ) ; 980*def = ora ; // changes the value of 'b' to a copy of the value of 'a' 981assert ( b == a ) ; 982int c = 3; 983int& rc = c ; 984optional<int&> orc(rc) ; 985ora = orc ; // REBINDS ora to 'c' through 'rc' 986c = 4 ; 987assert ( *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`` 1020int a = 1 ; 1021int b = 2 ; 1022T& ra = a ; 1023T& rb = b ; 1024optional<int&> def ; 1025optional<int&> opt(ra) ; 1026 1027def = rb ; // binds 'def' to 'b' through 'rb' 1028assert ( *def == b ) ; 1029*def = a ; // changes the value of 'b' to a copy of the value of 'a' 1030assert ( b == a ) ; 1031int c = 3; 1032int& rc = c ; 1033opt = rc ; // REBINDS to 'c' through 'rc' 1034c = 4 ; 1035assert ( *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`` 1057T v ; 1058T& vref = v ; 1059optional<T&> opt ( vref ); 1060T const& vref2 = *opt; 1061assert ( vref2 == v ) ; 1062++ v ; 1063assert ( *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:] Same as `*this = none`. 1133 1134__SPACE__ 1135 1136[#reference_optional_ref_reset_value] 1137[: `template<class R> void optional<T&>::reset ( R&& r) noexcept;`] 1138* [*Effects:] Equivalent to `*this = std::forward<R>(r)`. 1139* [*Remarks:] This function is depprecated. 1140 1141__SPACE__ 1142 1143[#reference_optional_ref_is_initialized] 1144[: `bool optional<T&>::is_initialized() const noexcept;`] 1145* [*Effects:] Equivalent to `return bool(*this)`. 1146* [*Remarks:] This function is depprecated. 1147 1148__SPACE__ 1149 1150[#reference_optional_ref_get_value_or_value] 1151[: `template<class R> T& optional<T&>::get_value_or( R&& r ) const noexcept;`] 1152* [*Effects:] Equivalent to `return value_or(std::forward<R>(r);`. 1153* [*Remarks:] This function is depprecated. 1154 1155[endsect] 1156 1157 1158[section Detailed Semantics - Free Functions] 1159 1160 1161__SPACE__ 1162 1163[#reference_make_optional_value] 1164 1165[: `optional<T> make_optional( T const& v )`] 1166 1167* [*Returns: ] `optional<T>(v)` for the ['deduced] type `T` of `v`. 1168* [*Example:] 1169`` 1170template<class T> void foo ( optional<T> const& opt ) ; 1171 1172foo ( make_optional(1+1) ) ; // Creates an optional<int> 1173`` 1174 1175__SPACE__ 1176 1177[#reference_make_optional_rvalue] 1178 1179[: `optional<std::decay_t<T>> make_optional( T && v )`] 1180 1181* [*Returns: ] `optional<std::decay_t<T>>(std::move(v))` for the ['deduced] type `T` of `v`. 1182 1183 1184__SPACE__ 1185 1186[#reference_make_optional_bool_value] 1187 1188[: `optional<T> make_optional( bool condition, T const& v )`] 1189 1190* [*Returns: ] `optional<T>(condition, v)` for the ['deduced] type `T` of `v`. 1191* [*Example:] 1192`` 1193optional<double> calculate_foo() 1194{ 1195 double val = compute_foo(); 1196 return make_optional(is_not_nan_and_finite(val),val); 1197} 1198 1199optional<double> v = calculate_foo(); 1200if ( !v ) 1201 error("foo wasn't computed"); 1202`` 1203 1204__SPACE__ 1205 1206[#reference_make_optional_bool_rvalue] 1207 1208[: `optional<std::decay_t<T>> make_optional( bool condition, T && v )`] 1209 1210* [*Returns: ] `optional<std::decay_t<T>>(condition, std::move(v))` for the ['deduced] type `T` of `v`. 1211 1212 1213__SPACE__ 1214 1215[#reference_operator_compare_equal_optional_optional] 1216 1217[: `bool operator == ( optional<T> const& x, optional<T> const& y );`] 1218 1219* [*Requires:] `T` shall meet requirements of __SGI_EQUALITY_COMPARABLE__. 1220* [*Returns:] If both `x` and `y` are initialized, `(*x == *y)`. If only 1221`x` or `y` is initialized, `false`. If both are uninitialized, `true`. 1222* [*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. 1223Pointers 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 1224__FUNCTION_EQUAL_POINTEES__ instead 1225* [*Example:] 1226`` 1227optional<T> oN, oN_; 1228optional<T> o1(T(1)), o1_(T(1)); 1229optional<T> o2(T(2)); 1230 1231assert ( oN == oN ); // Identity implies equality 1232assert ( o1 == o1 ); // 1233 1234assert ( oN == oN_ ); // Both uninitialized compare equal 1235 1236assert ( oN != o1 ); // Initialized unequal to initialized. 1237 1238assert ( o1 == o1_ ); // Both initialized compare as (*lhs == *rhs) 1239assert ( o1 != o2 ); // 1240`` 1241 1242__SPACE__ 1243 1244[#reference_operator_compare_less_optional_optional] 1245 1246[: `bool operator < ( optional<T> const& x, optional<T> const& y );`] 1247 1248* [*Requires:] Expression `*x < *y` shall be well-formed and its result shall be convertible to `bool`. 1249* [*Returns:] `(!y) ? false : (!x) ? true : *x < *y`. 1250* [*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. 1251Pointers have shallow relational operators while `optional` has deep relational operators. Do not use `operator<` directly in generic code 1252which 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__. 1253* [*Example:] 1254`` 1255optional<T> oN, oN_; 1256optional<T> o0(T(0)); 1257optional<T> o1(T(1)); 1258 1259assert ( !(oN < oN) ); // Identity implies equivalence 1260assert ( !(o1 < o1) ); 1261 1262assert ( !(oN < oN_) ); // Two uninitialized are equivalent 1263assert ( !(oN_ < oN) ); 1264 1265assert ( oN < o0 ); // Uninitialized is less than initialized 1266assert ( !(o0 < oN) ); 1267 1268assert ( o1 < o2 ) ; // Two initialized compare as (*lhs < *rhs) 1269assert ( !(o2 < o1) ) ; 1270assert ( !(o2 < o2) ) ; 1271`` 1272 1273__SPACE__ 1274 1275[#reference_operator_compare_not_equal_optional_optional] 1276 1277[: `bool operator != ( optional<T> const& x, optional<T> const& y );`] 1278 1279* [*Returns: ] `!( x == y );` 1280 1281__SPACE__ 1282 1283[#reference_operator_compare_greater_optional_optional] 1284 1285[: `bool operator > ( optional<T> const& x, optional<T> const& y );`] 1286 1287* [*Returns: ] `( y < x );` 1288 1289__SPACE__ 1290 1291[#reference_operator_compare_less_or_equal_optional_optional] 1292 1293[: `bool operator <= ( optional<T> const& x, optional<T> const& y );`] 1294 1295* [*Returns: ] `!( y < x );` 1296 1297__SPACE__ 1298 1299[#reference_operator_compare_greater_or_equal_optional_optional] 1300 1301[: `bool operator >= ( optional<T> const& x, optional<T> const& y );`] 1302 1303* [*Returns: ] `!( x < y );` 1304 1305__SPACE__ 1306 1307[#reference_operator_compare_equal_optional_none] 1308 1309[: `bool operator == ( optional<T> const& x, none_t ) noexcept;`] 1310[: `bool operator == ( none_t, optional<T> const& x ) noexcept;`] 1311 1312* [*Returns:] `!x`. 1313* [*Notes:] `T` need not meet requirements of __SGI_EQUALITY_COMPARABLE__. 1314 1315 1316__SPACE__ 1317 1318[#reference_operator_compare_not_equal_optional_none] 1319 1320[: `bool operator != ( optional<T> const& x, none_t ) noexcept;`] 1321[: `bool operator != ( none_t, optional<T> const& x ) noexcept;`] 1322 1323* [*Returns: ] `bool(x);` 1324 1325 1326__SPACE__ 1327 1328 1329[#reference_free_get_pointer] 1330[: `auto get_pointer ( optional<T>& o ) -> typename optional<T>::pointer_type ;`] 1331[: `auto get_pointer ( optional<T> const& o ) -> typename optional<T>::pointer_const_type ;`] 1332* [*Returns:] `o.get_ptr()`. 1333* [*Throws:] Nothing. 1334 1335__SPACE__ 1336 1337 1338[#reference_free_get_value_or] 1339[: `auto get_optional_value_or ( optional<T>& o, typename optional<T>::reference_type def ) -> typename optional<T>::reference_type ;`] 1340[: `auto get_optional_value_or ( optional<T> const& o, typename optional<T>::reference_const_type def ) -> typename optional<T>::reference_const_type ;`] 1341* [*Returns:] `o.get_value_or(def)`. 1342* [*Throws:] Nothing. 1343* [*Remarks:] This function is deprecated. 1344 1345__SPACE__ 1346 1347[#reference_swap_optional_optional] 1348 1349[: `void swap ( optional<T>& x, optional<T>& y ) ;`] 1350 1351* [*Requires:] Lvalues of type `T` shall be swappable and `T` shall be __MOVE_CONSTRUCTIBLE__. 1352* [*Effects:] 1353[table 1354 [] 1355 [[][[*`*this` contains a value]][[*`*this` does not contain a value]]] 1356 [[[*`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]] 1357 [[[*`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]] 1358] 1359* [*Postconditions:] The states of `x` and `y` interchanged. 1360* [*Throws:] If both are initialized, whatever `swap(T&,T&)` throws. If only 1361one is initialized, whatever `T::T ( T&& )` throws. 1362* [*Example:] 1363`` 1364T x(12); 1365T y(21); 1366optional<T> def0 ; 1367optional<T> def1 ; 1368optional<T> optX(x); 1369optional<T> optY(y); 1370 1371boost::swap(def0,def1); // no-op 1372 1373boost::swap(def0,optX); 1374assert ( *def0 == x ); 1375assert ( !optX ); 1376 1377boost::swap(def0,optX); // Get back to original values 1378 1379boost::swap(optX,optY); 1380assert ( *optX == y ); 1381assert ( *optY == x ); 1382`` 1383 1384__SPACE__ 1385 1386[#reference_swap_optional_reference] 1387[: `void swap ( optional<T&>& x, optional<T&>& y ) noexcept ;`] 1388 1389* [*Postconditions:] `x` refers to what `y` refererred to before the swap (if anything). `y` refers to whatever `x` referred to before the swap. 1390 1391* [*Example:] 1392`` 1393T x(12); 1394T y(21); 1395 1396optional<T&> opt0; 1397optional<T&> optX (x); 1398optional<T&> optY (y); 1399 1400boost::swap(optX, optY); 1401assert (addressof(*optX) == addressof(y)); 1402assert (addressof(*optY) == addressof(x)); 1403 1404boost::swap(opt0, optX); 1405assert ( opt0 ); 1406assert ( !optX ); 1407assert (addressof(*opt0) == addressof(y)); 1408`` 1409 1410[endsect] 1411