4 Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
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)
21 // (If T is of reference type, the parameters and results by reference are by value)
23 optional () ; ``[link reference_optional_constructor __GO_TO__]``
25 optional ( none_t ) ; ``[link reference_optional_constructor_none_t __GO_TO__]``
27 optional ( T const& v ) ; ``[link reference_optional_constructor_value __GO_TO__]``
30 optional ( bool condition, T const& v ) ; ``[link reference_optional_constructor_bool_value __GO_TO__]``
32 optional ( optional const& rhs ) ; ``[link reference_optional_constructor_optional __GO_TO__]``
34 template<class U> explicit optional ( optional<U> const& rhs ) ; ``[link reference_optional_constructor_other_optional __GO_TO__]``
36 template<class InPlaceFactory> explicit optional ( InPlaceFactory const& f ) ; ``[link reference_optional_constructor_factory __GO_TO__]``
38 template<class TypedInPlaceFactory> explicit optional ( TypedInPlaceFactory const& f ) ; ``[link reference_optional_constructor_factory __GO_TO__]``
40 optional& operator = ( none_t ) ; ``[/[link reference_optional_operator_equal_none_t __GO_TO__]]``
42 optional& operator = ( T const& v ) ; ``[link reference_optional_operator_equal_value __GO_TO__]``
44 optional& operator = ( optional const& rhs ) ; ``[link reference_optional_operator_equal_optional __GO_TO__]``
46 template<class U> optional& operator = ( optional<U> const& rhs ) ; ``[link reference_optional_operator_equal_other_optional __GO_TO__]``
48 template<class InPlaceFactory> optional& operator = ( InPlaceFactory const& f ) ; ``[/[link reference_optional_operator_equal_factory __GO_TO__]]``
50 template<class TypedInPlaceFactory> optional& operator = ( TypedInPlaceFactory const& f ) ; ``[/[link reference_optional_operator_equal_factory __GO_TO__]]``
52 T const& get() const ; ``[link reference_optional_get __GO_TO__]``
53 T& get() ; ``[link reference_optional_get __GO_TO__]``
56 T const& get_value_or( T const& default ) const ; ``[link reference_optional_get_value_or_value __GO_TO__]``
58 T const* operator ->() const ; ``[link reference_optional_operator_arrow __GO_TO__]``
59 T* operator ->() ; ``[link reference_optional_operator_arrow __GO_TO__]``
61 T const& operator *() const ; ``[link reference_optional_get __GO_TO__]``
62 T& operator *() ; ``[link reference_optional_get __GO_TO__]``
64 T const* get_ptr() const ; ``[link reference_optional_get_ptr __GO_TO__]``
65 T* get_ptr() ; ``[link reference_optional_get_ptr __GO_TO__]``
67 operator unspecified-bool-type() const ; ``[link reference_optional_operator_bool __GO_TO__]``
69 bool operator!() const ; ``[link reference_optional_operator_not __GO_TO__]``
74 void reset() ; ``[link reference_optional_reset __GO_TO__]``
77 void reset ( T const& ) ; ``[link reference_optional_reset_value __GO_TO__]``
80 bool is_initialized() const ; ``[link reference_optional_is_initialized __GO_TO__]``
84 template<class T> inline bool operator == ( optional<T> const& x, optional<T> const& y ) ; ``[link reference_operator_compare_equal_optional_optional __GO_TO__]``
86 template<class T> inline bool operator != ( optional<T> const& x, optional<T> const& y ) ; ``[link reference_operator_compare_not_equal_optional_optional __GO_TO__]``
88 template<class T> inline bool operator < ( optional<T> const& x, optional<T> const& y ) ; ``[link reference_operator_compare_less_optional_optional __GO_TO__]``
90 template<class T> inline bool operator > ( optional<T> const& x, optional<T> const& y ) ; ``[link reference_operator_compare_greater_optional_optional __GO_TO__]``
92 template<class T> inline bool operator <= ( optional<T> const& x, optional<T> const& y ) ; ``[link reference_operator_compare_less_or_equal_optional_optional __GO_TO__]``
94 template<class T> inline bool operator >= ( optional<T> const& x, optional<T> const& y ) ; ``[link reference_operator_compare_greater_or_equal_optional_optional __GO_TO__]``
97 template<class T> inline optional<T> make_optional ( T const& v ) ; ``[link reference_make_optional_value __GO_TO__]``
100 template<class T> inline optional<T> make_optional ( bool condition, T const& v ) ; ``[link reference_make_optional_bool_value __GO_TO__]``
103 template<class T> inline T const& get_optional_value_or ( optional<T> const& opt, T const& default ) ; ``[link reference_optional_get_value_or_value __GO_TO__]``
105 template<class T> inline T const& get ( optional<T> const& opt ) ; ``[link reference_optional_get __GO_TO__]``
107 template<class T> inline T& get ( optional<T> & opt ) ; ``[link reference_optional_get __GO_TO__]``
109 template<class T> inline T const* get ( optional<T> const* opt ) ; ``[link reference_optional_get __GO_TO__]``
111 template<class T> inline T* get ( optional<T>* opt ) ; ``[link reference_optional_get __GO_TO__]``
113 template<class T> inline T const* get_pointer ( optional<T> const& opt ) ; ``[link reference_optional_get_ptr __GO_TO__]``
115 template<class T> inline T* get_pointer ( optional<T> & opt ) ; ``[link reference_optional_get_ptr __GO_TO__]``
117 template<class T> inline void swap( optional<T>& x, optional<T>& y ) ; ``[link reference_swap_optional_optional __GO_TO__]``
124 [section Detailed Semantics]
126 Because `T` might be of reference type, in the sequel, those entries whose
127 semantic depends on `T` being of reference type or not will be distinguished
128 using the following convention:
130 * If the entry reads: `optional<T`['(not a ref)]`>`, the description
131 corresponds only to the case where `T` is not of reference type.
132 * If the entry reads: `optional<T&>`, the description corresponds only to
133 the case where `T` is of reference type.
134 * If the entry reads: `optional<T>`, the description is the same for both
138 The following section contains various `assert()` which are used only to show
139 the postconditions as sample code. It is not implied that the type `T` must
140 support each particular expression but that if the expression is supported,
141 the implied condition holds.
146 [heading optional class member functions]
150 [#reference_optional_constructor]
152 [: `optional<T>::optional();`]
154 * [*Effect:] Default-Constructs an `optional`.
155 * [*Postconditions:] `*this` is [_uninitialized].
156 * [*Throws:] Nothing.
157 * Notes: T's default constructor [_is not] called.
166 [#reference_optional_constructor_none_t]
168 [: `optional<T>::optional( none_t );`]
170 * [*Effect:] Constructs an `optional` uninitialized.
171 * [*Postconditions:] `*this` is [_uninitialized].
172 * [*Throws:] Nothing.
173 * [*Notes:] `T`'s default constructor [_is not] called. The expression
174 `boost::none` denotes an instance of `boost::none_t` that can be used as
178 #include <boost/none.hpp>
179 optional<T> n(none) ;
185 [#reference_optional_constructor_value]
187 [: `optional<T `['(not a ref)]`>::optional( T const& v )`]
189 * [*Effect:] Directly-Constructs an `optional`.
190 * [*Postconditions:] `*this` is [_initialized] and its value is a['copy]
192 * [*Throws:] Whatever `T::T( T const& )` throws.
193 * [*Notes: ] `T::T( T const& )` is called.
194 * [*Exception Safety:] Exceptions can only be thrown during
195 `T::T( T const& );` in that case, this constructor has no effect.
200 assert ( *opt == v ) ;
205 [: `optional<T&>::optional( T& ref )`]
207 * [*Effect:] Directly-Constructs an `optional`.
208 * [*Postconditions:] `*this` is [_initialized] and its value is an instance
209 of an internal type wrapping the reference `ref`.
210 * [*Throws:] Nothing.
215 optional<T&> opt(vref);
216 assert ( *opt == v ) ;
217 ++ v ; // mutate referee
223 [#reference_optional_constructor_bool_value]
225 [: `optional<T` ['(not a ref)]`>::optional( bool condition, T const& v ) ;` ]
226 [: `optional<T&> ::optional( bool condition, T& v ) ;` ]
228 * If condition is true, same as:
230 [: `optional<T` ['(not a ref)]`>::optional( T const& v )`]
231 [: `optional<T&> ::optional( T& v )`]
233 * otherwise, same as:
235 [: `optional<T ['(not a ref)]>::optional()`]
236 [: `optional<T&> ::optional()`]
240 [#reference_optional_constructor_optional]
242 [: `optional<T `['(not a ref)]`>::optional( optional const& rhs );`]
244 * [*Effect:] Copy-Constructs an `optional`.
245 * [*Postconditions:] If rhs is initialized, `*this` is initialized and
246 its value is a ['copy] of the value of `rhs`; else `*this` is uninitialized.
247 * [*Throws:] Whatever `T::T( T const& )` throws.
248 * [*Notes:] If rhs is initialized, `T::T(T const& )` is called.
249 * [*Exception Safety:] Exceptions can only be thrown during
250 `T::T( T const& );` in that case, this constructor has no effect.
256 optional<T> uinit2 ( uninit ) ;
257 assert ( uninit2 == uninit );
259 optional<T> init( T(2) );
260 assert ( *init == T(2) ) ;
262 optional<T> init2 ( init ) ;
263 assert ( init2 == init ) ;
268 [: `optional<T&>::optional( optional const& rhs );`]
270 * [*Effect:] Copy-Constructs an `optional`.
271 * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its
272 value is another reference to the same object referenced by `*rhs`; else
273 `*this` is uninitialized.
274 * [*Throws:] Nothing.
275 * [*Notes:] If `rhs` is initialized, both `*this` and `*rhs` will reefer to the
276 same object (they alias).
279 optional<T&> uninit ;
282 optional<T&> uinit2 ( uninit ) ;
283 assert ( uninit2 == uninit );
285 T v = 2 ; T& ref = v ;
286 optional<T> init(ref);
287 assert ( *init == v ) ;
289 optional<T> init2 ( init ) ;
290 assert ( *init2 == v ) ;
294 assert ( *init == 3 ) ;
295 assert ( *init2 == 3 ) ;
300 [#reference_optional_constructor_other_optional]
302 [: `template<U> explicit optional<T` ['(not a ref)]`>::optional( optional<U> const& rhs );`]
304 * [*Effect:] Copy-Constructs an `optional`.
305 * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its
306 value is a ['copy] of the value of rhs converted to type `T`; else `*this` is
308 * [*Throws:] Whatever `T::T( U const& )` throws.
309 * [*Notes: ] `T::T( U const& )` is called if `rhs` is initialized, which requires a
310 valid conversion from `U` to `T`.
311 * [*Exception Safety:] Exceptions can only be thrown during `T::T( U const& );`
312 in that case, this constructor has no effect.
315 optional<double> x(123.4);
316 assert ( *x == 123.4 ) ;
319 assert( *y == 123 ) ;
324 [#reference_optional_constructor_factory]
326 [: `template<InPlaceFactory> explicit optional<T` ['(not a ref)]`>::optional( InPlaceFactory const& f );`]
327 [: `template<TypedInPlaceFactory> explicit optional<T` ['(not a ref)]`>::optional( TypedInPlaceFactory const& f );`]
329 * [*Effect:] Constructs an `optional` with a value of `T` obtained from the
331 * [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given]
332 from the factory `f` (i.e., the value [_is not copied]).
333 * [*Throws:] Whatever the `T` constructor called by the factory throws.
334 * [*Notes:] See [link boost_optional.in_place_factories In-Place Factories]
335 * [*Exception Safety:] Exceptions can only be thrown during the call to
336 the `T` constructor used by the factory; in that case, this constructor has
340 class C { C ( char, double, std::string ) ; } ;
342 C v('A',123.4,"hello");
344 optional<C> x( in_place ('A', 123.4, "hello") ); // InPlaceFactory used
345 optional<C> y( in_place<C>('A', 123.4, "hello") ); // TypedInPlaceFactory used
353 [#reference_optional_operator_equal_value]
355 [: `optional& optional<T` ['(not a ref)]`>::operator= ( T const& rhs ) ;`]
357 * [*Effect:] Assigns the value `rhs` to an `optional`.
358 * [*Postconditions: ] `*this` is initialized and its value is a ['copy] of `rhs`.
359 * [*Throws:] Whatever `T::operator=( T const& )` or `T::T(T const&)` throws.
360 * [*Notes:] If `*this` was initialized, `T`'s assignment operator is used,
361 otherwise, its copy-constructor is used.
362 * [*Exception Safety:] In the event of an exception, the initialization
363 state of `*this` is unchanged and its value unspecified as far as `optional`
364 is concerned (it is up to `T`'s `operator=()`). If `*this` is initially
365 uninitialized and `T`'s ['copy constructor] fails, `*this` is left properly
375 assert ( *def == y ) ;
377 assert ( *opt == y ) ;
382 [: `optional<T&>& optional<T&>::operator= ( T& const& rhs ) ;`]
384 * [*Effect:] (Re)binds thee wrapped reference.
385 * [*Postconditions: ] `*this` is initialized and it references the same
386 object referenced by `rhs`.
387 * [*Notes:] If `*this` was initialized, is is ['rebound] to the new object.
388 See [link boost_optional.rebinding_semantics_for_assignment_of_optional_references here] for details on this behavior.
396 optional<int&> opt(ra) ;
398 def = rb ; // binds 'def' to 'b' through 'rb'
399 assert ( *def == b ) ;
400 *def = a ; // changes the value of 'b' to a copy of the value of 'a'
404 opt = rc ; // REBINDS to 'c' through 'rc'
406 assert ( *opt == 4 ) ;
411 [#reference_optional_operator_equal_optional]
413 [: `optional& optional<T` ['(not a ref)]`>::operator= ( optional const& rhs ) ;`]
415 * [*Effect:] Assigns another `optional` to an `optional`.
416 * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and
417 its value is a ['copy] of the value of `rhs`; else `*this` is uninitialized.
418 * [*Throws:] Whatever `T::operator( T const&)` or `T::T( T const& )` throws.
419 * [*Notes:] If both `*this` and `rhs` are initially initialized, `T`'s
420 ['assignment operator] is used. If `*this` is initially initialized but `rhs` is
421 uninitialized, `T`'s [destructor] is called. If `*this` is initially uninitialized
422 but `rhs` is initialized, `T`'s ['copy constructor] is called.
423 * [*Exception Safety:] In the event of an exception, the initialization state of
424 `*this` is unchanged and its value unspecified as far as optional is concerned
425 (it is up to `T`'s `operator=()`). If `*this` is initially uninitialized and
426 `T`'s ['copy constructor] fails, `*this` is left properly uninitialized.
435 // previous value (copy of 'v') destroyed from within 'opt'.
440 [: `optional<T&> & optional<T&>::operator= ( optional<T&> const& rhs ) ;`]
442 * [*Effect:] (Re)binds thee wrapped reference.
443 * [*Postconditions:] If `*rhs` is initialized, `*this` is initialized and it
444 references the same object referenced by `*rhs`; otherwise, `*this` is
445 uninitialized (and references no object).
446 * [*Notes:] If `*this` was initialized and so is *rhs, this is is ['rebound] to
447 the new object. See [link boost_optional.rebinding_semantics_for_assignment_of_optional_references here] for details on this behavior.
455 optional<int&> ora(ra) ;
456 optional<int&> orb(rb) ;
458 def = orb ; // binds 'def' to 'b' through 'rb' wrapped within 'orb'
459 assert ( *def == b ) ;
460 *def = ora ; // changes the value of 'b' to a copy of the value of 'a'
464 optional<int&> orc(rc) ;
465 ora = orc ; // REBINDS ora to 'c' through 'rc'
467 assert ( *ora == 4 ) ;
472 [#reference_optional_operator_equal_other_optional]
474 [: `template<U> optional& optional<T` ['(not a ref)]`>::operator= ( optional<U> const& rhs ) ;`]
476 * [*Effect:] Assigns another convertible optional to an optional.
477 * [*Postconditions:] If `rhs` is initialized, `*this` is initialized and
478 its value is a ['copy] of the value of `rhs` ['converted] to type `T`; else
479 `*this` is uninitialized.
480 * [*Throws:] Whatever `T::operator=( U const& )` or `T::T( U const& )` throws.
481 * [*Notes:] If both `*this` and rhs are initially initialized, `T`'s
482 ['assignment operator] (from `U`) is used. If `*this` is initially initialized
483 but `rhs` is uninitialized, `T`'s ['destructor] is called. If `*this` is
484 initially uninitialized but rhs is initialized, `T`'s ['converting constructor]
485 (from `U`) is called.
486 * [*Exception Safety:] In the event of an exception, the initialization state
487 of `*this` is unchanged and its value unspecified as far as optional is
488 concerned (it is up to `T`'s `operator=()`). If `*this` is initially
489 uninitialized and `T`'s converting constructor fails, `*this` is left properly
498 assert ( *opt1 == static_cast<U>(v) ) ;
503 [#reference_optional_reset_value]
505 [: `void optional<T` ['(not a ref)]`>::reset( T const& v ) ;`]
506 * [*Deprecated:] same as `operator= ( T const& v) ;`
510 [#reference_optional_reset]
512 [: `void optional<T>::reset() ;`]
513 * [*Deprecated:] Same as `operator=( detail::none_t );`
517 [#reference_optional_get]
519 [: `T const& optional<T` ['(not a ref)]`>::operator*() const ;`]
520 [: `T& optional<T` ['(not a ref)]`>::operator*();`]
521 [: `T const& optional<T` ['(not a ref)]`>::get() const ;`]
522 [: `T& optional<T` ['(not a ref)]`>::get() ;`]
524 [: `inline T const& get ( optional<T` ['(not a ref)]`> const& ) ;`]
525 [: `inline T& get ( optional<T` ['(not a ref)]`> &) ;`]
527 * [*Requirements:] `*this` is initialized
528 * [*Returns:] A reference to the contained value
529 * [*Throws:] Nothing.
530 * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`.
534 optional<T> opt ( v );
539 assert ( *opt == w ) ;
544 [#reference_optional_get_value_or_value]
546 [: `T const& optional<T` ['(not a ref)]`>::get_value_or( T const& default) const ;`]
547 [: `T& optional<T` ['(not a ref)]`>::get_value_or( T& default ) ;`]
549 [: `inline T const& get_optional_value_or ( optional<T` ['(not a ref)]`> const& o, T const& default ) ;`]
550 [: `inline T& get_optional_value_or ( optional<T` ['(not a ref)]`>& o, T& default ) ;`]
552 * [*Returns:] A reference to the contained value, if any, or `default`.
553 * [*Throws:] Nothing.
558 T const& y = def.get_value_or(z);
561 optional<T> opt ( v );
562 T const& u = get_optional_value_or(opt,z);
569 [: `T const& optional<T&>::operator*() const ;`]
570 [: `T & optional<T&>::operator*();`]
571 [: `T const& optional<T&>::get() const ;`]
572 [: `T& optional<T&>::get() ;`]
574 [: `inline T const& get ( optional<T&> const& ) ;`]
575 [: `inline T& get ( optional<T&> &) ;`]
577 * [*Requirements: ] `*this` is initialized
578 * [*Returns:] [_The] reference contained.
579 * [*Throws:] Nothing.
580 * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`.
585 optional<T&> opt ( vref );
586 T const& vref2 = *opt;
587 assert ( vref2 == v ) ;
589 assert ( *opt == v ) ;
594 [#reference_optional_get_ptr]
596 [: `T const* optional<T` ['(not a ref)]`>::get_ptr() const ;`]
597 [: `T* optional<T` ['(not a ref)]`>::get_ptr() ;`]
599 [: `inline T const* get_pointer ( optional<T` ['(not a ref)]`> const& ) ;`]
600 [: `inline T* get_pointer ( optional<T` ['(not a ref)]`> &) ;`]
602 * [*Returns:] If `*this` is initialized, a pointer to the contained value;
604 * [*Throws:] Nothing.
605 * [*Notes:] The contained value is permanently stored within `*this`, so you
606 should not hold nor delete this pointer
611 optional<T> const copt(v);
612 T* p = opt.get_ptr() ;
613 T const* cp = copt.get_ptr();
614 assert ( p == get_pointer(opt) );
615 assert ( cp == get_pointer(copt) ) ;
620 [#reference_optional_operator_arrow]
622 [: `T const* optional<T` ['(not a ref)]`>::operator ->() const ;`]
623 [: `T* optional<T` ['(not a ref)]`>::operator ->() ;`]
625 * [*Requirements: ] `*this` is initialized.
626 * [*Returns:] A pointer to the contained value.
627 * [*Throws:] Nothing.
628 * [*Notes:] The requirement is asserted via `BOOST_ASSERT()`.
631 struct X { int mdata ; } ;
639 [#reference_optional_operator_bool]
641 [: `optional<T>::operator `['unspecified-bool-type]`() const ;`]
643 * [*Returns:] An unspecified value which if used on a boolean context
644 is equivalent to (`get() != 0`)
645 * [*Throws:] Nothing.
650 optional<T> opt ( v ) ;
657 [#reference_optional_operator_not]
659 [: `bool optional<T>::operator!() ;`]
661 * [*Returns:] If `*this` is uninitialized, `true`; else `false`.
662 * [*Throws:] Nothing.
663 * [*Notes:] This operator is provided for those compilers which can't
664 use the ['unspecified-bool-type operator] in certain boolean contexts.
671 // Notice the "double-bang" idiom here.
677 [#reference_optional_is_initialized]
679 [: `bool optional<T>::is_initialized() const ;`]
681 * [*Returns: ] `true` if the `optional` is initialized, `false` otherwise.
682 * [*Throws:] Nothing.
686 assert ( !def.is_initialized() );
687 optional<T> opt ( v ) ;
688 assert ( opt.is_initialized() );
693 [heading Free functions]
697 [#reference_make_optional_value]
699 [: `optional<T` ['(not a ref)]`> make_optional( T const& v )`]
701 * [*Returns: ] `optional<T>(v)` for the ['deduced] type `T` of `v`.
704 template<class T> void foo ( optional<T> const& opt ) ;
706 foo ( make_optional(1+1) ) ; // Creates an optional<int>
711 [#reference_make_optional_bool_value]
713 [: `optional<T` ['(not a ref)]`> make_optional( bool condition, T const& v )`]
715 * [*Returns: ] `optional<T>(condition,v)` for the ['deduced] type `T` of `v`.
718 optional<double> calculate_foo()
720 double val = compute_foo();
721 return make_optional(is_not_nan_and_finite(val),val);
724 optional<double> v = calculate_foo();
726 error("foo wasn't computed");
731 [#reference_operator_compare_equal_optional_optional]
733 [: `bool operator == ( optional<T> const& x, optional<T> const& y );`]
735 * [*Returns:] If both `x` and `y` are initialized, `(*x == *y)`. If only
736 `x` or `y` is initialized, `false`. If both are uninitialized, `true`.
737 * [*Throws:] Nothing.
738 * [*Notes:] Pointers have shallow relational operators while `optional` has
739 deep relational operators. Do not use `operator ==` directly in generic
740 code which expect to be given either an `optional<T>` or a pointer; use
741 __FUNCTION_EQUAL_POINTEES__ instead
753 // Identity always hold
754 assert ( def0 == def0 );
755 assert ( optX == optX );
757 // Both uninitialized compare equal
758 assert ( def0 == def1 );
760 // Only one initialized compare unequal.
761 assert ( def0 != optX );
763 // Both initialized compare as (*lhs == *rhs)
764 assert ( optX == optY ) ;
765 assert ( optX != optZ ) ;
770 [#reference_operator_compare_less_optional_optional]
772 [: `bool operator < ( optional<T> const& x, optional<T> const& y );`]
774 * [*Returns:] If `y` is not initialized, `false`. If `y` is initialized
775 and `x` is not initialized, `true`. If both `x` and `y` are initialized,
777 * [*Throws:] Nothing.
778 * [*Notes:] Pointers have shallow relational operators while `optional` has
779 deep relational operators. Do not use `operator <` directly in generic code
780 which expect to be given either an `optional<T>` or a pointer; use __FUNCTION_LESS_POINTEES__ instead.
789 // Identity always hold
790 assert ( !(def < def) );
791 assert ( optX == optX );
793 // Both uninitialized compare equal
794 assert ( def0 == def1 );
796 // Only one initialized compare unequal.
797 assert ( def0 != optX );
799 // Both initialized compare as (*lhs == *rhs)
800 assert ( optX == optY ) ;
801 assert ( optX != optZ ) ;
806 [#reference_operator_compare_not_equal_optional_optional]
808 [: `bool operator != ( optional<T> const& x, optional<T> const& y );`]
810 * [*Returns: ] `!( x == y );`
811 * [*Throws:] Nothing.
815 [#reference_operator_compare_greater_optional_optional]
817 [: `bool operator > ( optional<T> const& x, optional<T> const& y );`]
819 * [*Returns: ] `( y < x );`
820 * [*Throws:] Nothing.
824 [#reference_operator_compare_less_or_equal_optional_optional]
826 [: `bool operator <= ( optional<T> const& x, optional<T> const& y );`]
828 * [*Returns: ] `!( y<x );`
829 * [*Throws:] Nothing.
833 [#reference_operator_compare_greater_or_equal_optional_optional]
835 [: `bool operator >= ( optional<T> const& x, optional<T> const& y );`]
837 * [*Returns: ] `!( x<y );`
838 * [*Throws:] Nothing.
842 [#reference_swap_optional_optional]
844 [: `void swap ( optional<T>& x, optional<T>& y );`]
846 * [*Effect:] If both `x` and `y` are initialized, calls `swap(*x,*y)`
847 using `std::swap`. If only one is initialized, say `x`, calls:
848 `y.reset(*x); x.reset();` If none is initialized, does nothing.
849 * [*Postconditions:] The states of `x` and `y` interchanged.
850 * [*Throws:] If both are initialized, whatever `swap(T&,T&)` throws. If only
851 one is initialized, whatever `T::T ( T const& )` throws.
852 * [*Notes:] If both are initialized, `swap(T&,T&)` is used unqualified but
853 with `std::swap` introduced in scope.
854 If only one is initialized, `T::~T()` and `T::T( T const& )` is called.
855 * [*Exception Safety:] If both are initialized, this operation has the
856 exception safety guarantees of `swap(T&,T&)`.
857 If only one is initialized, it has the same basic guarantee as
858 `optional<T>::reset( T const& )`.
868 boost::swap(def0,def1); // no-op
870 boost::swap(def0,optX);
871 assert ( *def0 == x );
874 boost::swap(def0,optX); // Get back to original values
876 boost::swap(optX,optY);
877 assert ( *optX == y );
878 assert ( *optY == x );