1 // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/optional for documentation.
9 // You are welcome to contact the author at:
10 // fernando_cacciola@hotmail.com
13 // 27 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen
15 #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
16 #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
21 #include <boost/config.hpp>
22 #include <boost/assert.hpp>
23 #include <boost/type.hpp>
24 #include <boost/type_traits/alignment_of.hpp>
25 #include <boost/type_traits/has_nothrow_constructor.hpp>
26 #include <boost/type_traits/type_with_alignment.hpp>
27 #include <boost/type_traits/remove_reference.hpp>
28 #include <boost/type_traits/is_reference.hpp>
29 #include <boost/mpl/if.hpp>
30 #include <boost/mpl/bool.hpp>
31 #include <boost/mpl/not.hpp>
32 #include <boost/detail/reference_content.hpp>
33 #include <boost/none.hpp>
34 #include <boost/utility/swap.hpp>
35 #include <boost/utility/addressof.hpp>
36 #include <boost/utility/compare_pointees.hpp>
37 #include <boost/utility/in_place_factory.hpp>
39 #include <boost/optional/optional_fwd.hpp>
41 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
42 // VC6.0 has the following bug:
43 // When a templated assignment operator exist, an implicit conversion
44 // constructing an optional<T> is used when assigment of the form:
45 // optional<T> opt ; opt = T(...);
47 // However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
48 // Therefore, for VC6.0 templated assignment is disabled.
50 #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
53 #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
54 // VC7.0 has the following bug:
55 // When both a non-template and a template copy-ctor exist
56 // and the templated version is made 'explicit', the explicit is also
57 // given to the non-templated version, making the class non-implicitely-copyable.
59 #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
62 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700)
63 // AFAICT only VC7.1 correctly resolves the overload set
64 // that includes the in-place factory taking functions,
65 // so for the other VC versions, in-place factory support
67 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
70 #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
71 // BCB (5.5.1) cannot parse the nested template struct in an inplace factory.
72 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
75 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \
76 && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581) )
77 // BCB (up to 5.64) has the following bug:
78 // If there is a member function/operator template of the form
79 // template<class Expr> mfunc( Expr expr ) ;
80 // some calls are resolved to this even if there are other better matches.
81 // The effect of this bug is that calls to converting ctors and assignments
82 // are incrorrectly sink to this general catch-all member function template as shown above.
83 #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
86 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) > 302 \
87 && !defined(__INTEL_COMPILER)
88 // GCC since 3.3 has may_alias attribute that helps to alleviate optimizer issues with
89 // regard to violation of the strict aliasing rules. The optional< T > storage type is marked
90 // with this attribute in order to let the compiler know that it will alias objects of type T
91 // and silence compilation warnings.
92 #define BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS
95 // Daniel Wallin discovered that bind/apply.hpp badly interacts with the apply<>
96 // member template of a factory as used in the optional<> implementation.
97 // He proposed this simple fix which is to move the call to apply<> outside
99 namespace boost_optional_detail
101 template <class T, class Factory>
102 inline void construct(Factory const& factory, void* address)
104 factory.BOOST_NESTED_TEMPLATE apply<T>(address);
111 class in_place_factory_base ;
112 class typed_in_place_factory_base ;
114 // This forward is needed to refer to namespace scope swap from the member swap
115 template<class T> void swap ( optional<T>& x, optional<T>& y );
117 namespace optional_detail {
119 // This local class is used instead of that in "aligned_storage.hpp"
120 // because I've found the 'official' class to ICE BCB5.5
121 // when some types are used with optional<>
122 // (due to sizeof() passed down as a non-type template parameter)
124 class aligned_storage
126 // Borland ICEs if unnamed unions are used for this!
128 // This works around GCC warnings about breaking strict aliasing rules when casting storage address to T*
129 #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS)
130 __attribute__((may_alias))
134 char data[ sizeof(T) ];
135 BOOST_DEDUCED_TYPENAME type_with_alignment<
136 ::boost::alignment_of<T>::value >::type aligner_;
141 #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS)
142 void const* address() const { return &dummy_; }
143 void * address() { return &dummy_; }
145 void const* address() const { return dummy_.data; }
146 void * address() { return dummy_.data; }
151 struct types_when_isnt_ref
153 typedef T const& reference_const_type ;
154 typedef T & reference_type ;
155 typedef T const* pointer_const_type ;
156 typedef T * pointer_type ;
157 typedef T const& argument_type ;
160 struct types_when_is_ref
162 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
164 typedef raw_type& reference_const_type ;
165 typedef raw_type& reference_type ;
166 typedef raw_type* pointer_const_type ;
167 typedef raw_type* pointer_type ;
168 typedef raw_type& argument_type ;
171 struct optional_tag {} ;
174 class optional_base : public optional_tag
179 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
180 BOOST_DEDUCED_TYPENAME
182 ::boost::detail::make_reference_content<T>::type internal_type ;
184 typedef aligned_storage<internal_type> storage_type ;
186 typedef types_when_isnt_ref<T> types_when_not_ref ;
187 typedef types_when_is_ref<T> types_when_ref ;
189 typedef optional_base<T> this_type ;
193 typedef T value_type ;
195 typedef mpl::true_ is_reference_tag ;
196 typedef mpl::false_ is_not_reference_tag ;
198 typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
201 typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
204 typedef bool (this_type::*unspecified_bool_type)() const;
206 typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
207 typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
208 typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
209 typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
210 typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
212 // Creates an optional<T> uninitialized.
216 m_initialized(false) {}
218 // Creates an optional<T> uninitialized.
220 optional_base ( none_t )
222 m_initialized(false) {}
224 // Creates an optional<T> initialized with 'val'.
225 // Can throw if T::T(T const&) does
226 optional_base ( argument_type val )
233 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional<T>.
234 // Can throw if T::T(T const&) does
235 optional_base ( bool cond, argument_type val )
243 // Creates a deep copy of another optional<T>
244 // Can throw if T::T(T const&) does
245 optional_base ( optional_base const& rhs )
249 if ( rhs.is_initialized() )
250 construct(rhs.get_impl());
254 // This is used for both converting and in-place constructions.
255 // Derived classes use the 'tag' to select the appropriate
256 // implementation (the correct 'construct()' overload)
258 explicit optional_base ( Expr const& expr, Expr const* tag )
267 // No-throw (assuming T::~T() doesn't)
268 ~optional_base() { destroy() ; }
270 // Assigns from another optional<T> (deep-copies the rhs value)
271 void assign ( optional_base const& rhs )
273 if (is_initialized())
275 if ( rhs.is_initialized() )
276 assign_value(rhs.get_impl(), is_reference_predicate() );
281 if ( rhs.is_initialized() )
282 construct(rhs.get_impl());
286 // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
288 void assign ( optional<U> const& rhs )
290 if (is_initialized())
292 if ( rhs.is_initialized() )
293 assign_value(static_cast<value_type>(rhs.get()), is_reference_predicate() );
298 if ( rhs.is_initialized() )
299 construct(static_cast<value_type>(rhs.get()));
303 // Assigns from a T (deep-copies the rhs value)
304 void assign ( argument_type val )
306 if (is_initialized())
307 assign_value(val, is_reference_predicate() );
311 // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
312 // No-throw (assuming T::~T() doesn't)
313 void assign ( none_t ) { destroy(); }
315 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
317 void assign_expr ( Expr const& expr, Expr const* tag )
319 if (is_initialized())
320 assign_expr_to_initialized(expr,tag);
321 else construct(expr,tag);
327 // Destroys the current value, if any, leaving this UNINITIALIZED
328 // No-throw (assuming T::~T() doesn't)
329 void reset() { destroy(); }
331 // Replaces the current value -if any- with 'val'
332 void reset ( argument_type val ) { assign(val); }
334 // Returns a pointer to the value if this is initialized, otherwise,
337 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
338 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
340 bool is_initialized() const { return m_initialized ; }
344 void construct ( argument_type val )
346 new (m_storage.address()) internal_type(val) ;
347 m_initialized = true ;
350 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
351 // Constructs in-place using the given factory
353 void construct ( Expr const& factory, in_place_factory_base const* )
355 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
356 boost_optional_detail::construct<value_type>(factory, m_storage.address());
357 m_initialized = true ;
360 // Constructs in-place using the given typed factory
362 void construct ( Expr const& factory, typed_in_place_factory_base const* )
364 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
365 factory.apply(m_storage.address()) ;
366 m_initialized = true ;
370 void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
373 construct(factory,tag);
376 // Constructs in-place using the given typed factory
378 void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
381 construct(factory,tag);
385 // Constructs using any expression implicitely convertible to the single argument
386 // of a one-argument T constructor.
387 // Converting constructions of optional<T> from optional<U> uses this function with
388 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
390 void construct ( Expr const& expr, void const* )
392 new (m_storage.address()) internal_type(expr) ;
393 m_initialized = true ;
396 // Assigns using a form any expression implicitely convertible to the single argument
397 // of a T's assignment operator.
398 // Converting assignments of optional<T> from optional<U> uses this function with
399 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
401 void assign_expr_to_initialized ( Expr const& expr, void const* )
403 assign_value(expr, is_reference_predicate());
406 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
407 // BCB5.64 (and probably lower versions) workaround.
408 // The in-place factories are supported by means of catch-all constructors
409 // and assignment operators (the functions are parameterized in terms of
410 // an arbitrary 'Expr' type)
411 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
412 // to the 'Expr'-taking functions even though explicit overloads are present for them.
413 // Thus, the following overload is needed to properly handle the case when the 'lhs'
414 // is another optional.
416 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
417 // instead of choosing the wrong overload
419 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
421 void construct ( Expr const& expr, optional_tag const* )
423 if ( expr.is_initialized() )
425 // An exception can be thrown here.
426 // It it happens, THIS will be left uninitialized.
427 new (m_storage.address()) internal_type(expr.get()) ;
428 m_initialized = true ;
433 void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
434 void assign_value ( argument_type val, is_reference_tag ) { construct(val); }
439 destroy_impl(is_reference_predicate()) ;
442 unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
444 reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
445 reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
447 pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
448 pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
452 // internal_type can be either T or reference_content<T>
453 #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS)
454 // This workaround is supposed to silence GCC warnings about broken strict aliasing rules
455 internal_type const* get_object() const
457 union { void const* ap_pvoid; internal_type const* as_ptype; } caster = { m_storage.address() };
458 return caster.as_ptype;
460 internal_type * get_object()
462 union { void* ap_pvoid; internal_type* as_ptype; } caster = { m_storage.address() };
463 return caster.as_ptype;
466 internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
467 internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
470 // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
471 reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
472 reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
473 reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
474 reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
476 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
477 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
479 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->T::~T() ; m_initialized = false ; }
482 void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
484 // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
485 // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
486 // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
487 pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
488 pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; }
489 pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; }
490 pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; }
493 storage_type m_storage ;
496 } // namespace optional_detail
499 class optional : public optional_detail::optional_base<T>
501 typedef optional_detail::optional_base<T> base ;
503 typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ;
507 typedef optional<T> this_type ;
509 typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
510 typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
511 typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
512 typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
513 typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
514 typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
516 // Creates an optional<T> uninitialized.
518 optional() : base() {}
520 // Creates an optional<T> uninitialized.
522 optional( none_t none_ ) : base(none_) {}
524 // Creates an optional<T> initialized with 'val'.
525 // Can throw if T::T(T const&) does
526 optional ( argument_type val ) : base(val) {}
528 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
529 // Can throw if T::T(T const&) does
530 optional ( bool cond, argument_type val ) : base(cond,val) {}
532 #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
533 // NOTE: MSVC needs templated versions first
535 // Creates a deep copy of another convertible optional<U>
536 // Requires a valid conversion from U to T.
537 // Can throw if T::T(U const&) does
539 explicit optional ( optional<U> const& rhs )
543 if ( rhs.is_initialized() )
544 this->construct(rhs.get());
548 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
549 // Creates an optional<T> with an expression which can be either
550 // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
551 // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
552 // (c) Any expression implicitely convertible to the single type
553 // of a one-argument T's constructor.
554 // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
555 // even though explicit overloads are present for these.
556 // Depending on the above some T ctor is called.
557 // Can throw is the resolved T ctor throws.
559 explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
562 // Creates a deep copy of another optional<T>
563 // Can throw if T::T(T const&) does
564 optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
566 // No-throw (assuming T::~T() doesn't)
569 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
570 // Assigns from an expression. See corresponding constructor.
571 // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
573 optional& operator= ( Expr const& expr )
575 this->assign_expr(expr,boost::addressof(expr));
581 #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
582 // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
583 // Requires a valid conversion from U to T.
584 // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
586 optional& operator= ( optional<U> const& rhs )
593 // Assigns from another optional<T> (deep-copies the rhs value)
594 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
595 // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
596 optional& operator= ( optional const& rhs )
598 this->assign( static_cast<base const&>(rhs) ) ;
602 // Assigns from a T (deep-copies the rhs value)
603 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
604 optional& operator= ( argument_type val )
606 this->assign( val ) ;
610 // Assigns from a "none"
611 // Which destroys the current value, if any, leaving this UNINITIALIZED
612 // No-throw (assuming T::~T() doesn't)
613 optional& operator= ( none_t none_ )
615 this->assign( none_ ) ;
619 void swap( optional & arg )
621 // allow for Koenig lookup
627 // Returns a reference to the value if this is initialized, otherwise,
628 // the behaviour is UNDEFINED
630 reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
631 reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
633 // Returns a copy of the value if this is initialized, 'v' otherwise
634 reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
635 reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
637 // Returns a pointer to the value if this is initialized, otherwise,
638 // the behaviour is UNDEFINED
640 pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
641 pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
643 // Returns a reference to the value if this is initialized, otherwise,
644 // the behaviour is UNDEFINED
646 reference_const_type operator *() const { return this->get() ; }
647 reference_type operator *() { return this->get() ; }
649 // implicit conversion to "bool"
651 operator unspecified_bool_type() const { return this->safe_bool() ; }
653 // This is provided for those compilers which don't like the conversion to bool
655 bool operator!() const { return !this->is_initialized() ; }
658 // Returns optional<T>(v)
661 optional<T> make_optional ( T const& v )
663 return optional<T>(v);
666 // Returns optional<T>(cond,v)
669 optional<T> make_optional ( bool cond, T const& v )
671 return optional<T>(cond,v);
674 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
678 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
679 get ( optional<T> const& opt )
686 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
687 get ( optional<T>& opt )
692 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
696 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
697 get ( optional<T> const* opt )
699 return opt->get_ptr() ;
704 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
705 get ( optional<T>* opt )
707 return opt->get_ptr() ;
710 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
714 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
715 get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
717 return opt.get_value_or(v) ;
722 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
723 get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
725 return opt.get_value_or(v) ;
728 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
732 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
733 get_pointer ( optional<T> const& opt )
735 return opt.get_ptr() ;
740 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
741 get_pointer ( optional<T>& opt )
743 return opt.get_ptr() ;
746 // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
747 // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
751 // optional<T> vs optional<T> cases
756 bool operator == ( optional<T> const& x, optional<T> const& y )
757 { return equal_pointees(x,y); }
761 bool operator < ( optional<T> const& x, optional<T> const& y )
762 { return less_pointees(x,y); }
766 bool operator != ( optional<T> const& x, optional<T> const& y )
767 { return !( x == y ) ; }
771 bool operator > ( optional<T> const& x, optional<T> const& y )
776 bool operator <= ( optional<T> const& x, optional<T> const& y )
777 { return !( y < x ) ; }
781 bool operator >= ( optional<T> const& x, optional<T> const& y )
782 { return !( x < y ) ; }
786 // optional<T> vs T cases
790 bool operator == ( optional<T> const& x, T const& y )
791 { return equal_pointees(x, optional<T>(y)); }
795 bool operator < ( optional<T> const& x, T const& y )
796 { return less_pointees(x, optional<T>(y)); }
800 bool operator != ( optional<T> const& x, T const& y )
801 { return !( x == y ) ; }
805 bool operator > ( optional<T> const& x, T const& y )
810 bool operator <= ( optional<T> const& x, T const& y )
811 { return !( y < x ) ; }
815 bool operator >= ( optional<T> const& x, T const& y )
816 { return !( x < y ) ; }
819 // T vs optional<T> cases
824 bool operator == ( T const& x, optional<T> const& y )
825 { return equal_pointees( optional<T>(x), y ); }
829 bool operator < ( T const& x, optional<T> const& y )
830 { return less_pointees( optional<T>(x), y ); }
834 bool operator != ( T const& x, optional<T> const& y )
835 { return !( x == y ) ; }
839 bool operator > ( T const& x, optional<T> const& y )
844 bool operator <= ( T const& x, optional<T> const& y )
845 { return !( y < x ) ; }
849 bool operator >= ( T const& x, optional<T> const& y )
850 { return !( x < y ) ; }
854 // optional<T> vs none cases
859 bool operator == ( optional<T> const& x, none_t )
860 { return equal_pointees(x, optional<T>() ); }
864 bool operator < ( optional<T> const& x, none_t )
865 { return less_pointees(x,optional<T>() ); }
869 bool operator != ( optional<T> const& x, none_t y )
870 { return !( x == y ) ; }
874 bool operator > ( optional<T> const& x, none_t y )
879 bool operator <= ( optional<T> const& x, none_t y )
880 { return !( y < x ) ; }
884 bool operator >= ( optional<T> const& x, none_t y )
885 { return !( x < y ) ; }
888 // none vs optional<T> cases
893 bool operator == ( none_t , optional<T> const& y )
894 { return equal_pointees(optional<T>() ,y); }
898 bool operator < ( none_t , optional<T> const& y )
899 { return less_pointees(optional<T>() ,y); }
903 bool operator != ( none_t x, optional<T> const& y )
904 { return !( x == y ) ; }
908 bool operator > ( none_t x, optional<T> const& y )
913 bool operator <= ( none_t x, optional<T> const& y )
914 { return !( y < x ) ; }
918 bool operator >= ( none_t x, optional<T> const& y )
919 { return !( x < y ) ; }
921 namespace optional_detail {
923 template<bool use_default_constructor> struct swap_selector;
926 struct swap_selector<true>
929 static void optional_swap ( optional<T>& x, optional<T>& y )
931 const bool hasX = !!x;
932 const bool hasY = !!y;
934 if ( !hasX && !hasY )
938 x = boost::in_place();
940 y = boost::in_place();
942 // Boost.Utility.Swap will take care of ADL and workarounds for broken compilers
943 boost::swap(x.get(),y.get());
953 struct swap_selector<false>
956 static void optional_swap ( optional<T>& x, optional<T>& y )
958 const bool hasX = !!x;
959 const bool hasY = !!y;
966 else if ( hasX && !hasY )
971 else if ( hasX && hasY )
973 // Boost.Utility.Swap will take care of ADL and workarounds for broken compilers
974 boost::swap(x.get(),y.get());
979 } // namespace optional_detail
982 struct optional_swap_should_use_default_constructor : has_nothrow_default_constructor<T> {} ;
984 template<class T> inline void swap ( optional<T>& x, optional<T>& y )
986 optional_detail::swap_selector<optional_swap_should_use_default_constructor<T>::value>::optional_swap(x, y);