1 // Copyright (C) 2003, 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
12 #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
13 #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
18 #include "boost/config.hpp"
19 #include "boost/assert.hpp"
20 #include "boost/type.hpp"
21 #include "boost/type_traits/alignment_of.hpp"
22 #include "boost/type_traits/type_with_alignment.hpp"
23 #include "boost/type_traits/remove_reference.hpp"
24 #include "boost/type_traits/is_reference.hpp"
25 #include "boost/mpl/if.hpp"
26 #include "boost/mpl/bool.hpp"
27 #include "boost/mpl/not.hpp"
28 #include "boost/detail/reference_content.hpp"
29 #include "boost/none.hpp"
30 #include "boost/utility/compare_pointees.hpp"
32 #include "boost/optional/optional_fwd.hpp"
34 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
35 // VC6.0 has the following bug:
36 // When a templated assignment operator exist, an implicit conversion
37 // constructing an optional<T> is used when assigment of the form:
38 // optional<T> opt ; opt = T(...);
40 // However, optional's ctor is _explicit_ and the assignemt shouldn't compile.
41 // Therefore, for VC6.0 templated assignment is disabled.
43 #define BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
46 #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
47 // VC7.0 has the following bug:
48 // When both a non-template and a template copy-ctor exist
49 // and the templated version is made 'explicit', the explicit is also
50 // given to the non-templated version, making the class non-implicitely-copyable.
52 #define BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
55 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700)
56 // AFAICT only VC7.1 correctly resolves the overload set
57 // that includes the in-place factory taking functions,
58 // so for the other VC versions, in-place factory support
60 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
63 #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
64 // BCB (5.5.1) cannot parse the nested template struct in an inplace factory.
65 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
68 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \
69 && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581) )
70 // BCB (up to 5.64) has the following bug:
71 // If there is a member function/operator template of the form
72 // template<class Expr> mfunc( Expr expr ) ;
73 // some calls are resolved to this even if there are other better matches.
74 // The effect of this bug is that calls to converting ctors and assignments
75 // are incrorrectly sink to this general catch-all member function template as shown above.
76 #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
79 // Daniel Wallin discovered that bind/apply.hpp badly interacts with the apply<>
80 // member template of a factory as used in the optional<> implementation.
81 // He proposed this simple fix which is to move the call to apply<> outside
83 namespace boost_optional_detail
85 template <class T, class Factory>
86 void construct(Factory const& factory, void* address)
88 factory.BOOST_NESTED_TEMPLATE apply<T>(address);
95 class in_place_factory_base ;
96 class typed_in_place_factory_base ;
98 namespace optional_detail {
100 // This local class is used instead of that in "aligned_storage.hpp"
101 // because I've found the 'official' class to ICE BCB5.5
102 // when some types are used with optional<>
103 // (due to sizeof() passed down as a non-type template parameter)
105 class aligned_storage
107 // Borland ICEs if unnamed unions are used for this!
110 char data[ sizeof(T) ];
111 BOOST_DEDUCED_TYPENAME type_with_alignment<
112 ::boost::alignment_of<T>::value >::type aligner_;
117 void const* address() const { return &dummy_.data[0]; }
118 void * address() { return &dummy_.data[0]; }
122 struct types_when_isnt_ref
124 typedef T const& reference_const_type ;
125 typedef T & reference_type ;
126 typedef T const* pointer_const_type ;
127 typedef T * pointer_type ;
128 typedef T const& argument_type ;
131 struct types_when_is_ref
133 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
135 typedef raw_type& reference_const_type ;
136 typedef raw_type& reference_type ;
137 typedef raw_type* pointer_const_type ;
138 typedef raw_type* pointer_type ;
139 typedef raw_type& argument_type ;
142 struct optional_tag {} ;
145 class optional_base : public optional_tag
150 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
151 BOOST_DEDUCED_TYPENAME
153 ::boost::detail::make_reference_content<T>::type internal_type ;
155 typedef aligned_storage<internal_type> storage_type ;
157 typedef types_when_isnt_ref<T> types_when_not_ref ;
158 typedef types_when_is_ref<T> types_when_ref ;
160 typedef optional_base<T> this_type ;
164 typedef T value_type ;
166 typedef mpl::true_ is_reference_tag ;
167 typedef mpl::false_ is_not_reference_tag ;
169 typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
172 typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
175 typedef bool (this_type::*unspecified_bool_type)() const;
177 typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
178 typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
179 typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
180 typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
181 typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
183 // Creates an optional<T> uninitialized.
187 m_initialized(false) {}
189 // Creates an optional<T> uninitialized.
191 optional_base ( none_t )
193 m_initialized(false) {}
195 // Creates an optional<T> initialized with 'val'.
196 // Can throw if T::T(T const&) does
197 optional_base ( argument_type val )
204 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional<T>.
205 // Can throw if T::T(T const&) does
206 optional_base ( bool cond, argument_type val )
214 // Creates a deep copy of another optional<T>
215 // Can throw if T::T(T const&) does
216 optional_base ( optional_base const& rhs )
220 if ( rhs.is_initialized() )
221 construct(rhs.get_impl());
225 // This is used for both converting and in-place constructions.
226 // Derived classes use the 'tag' to select the appropriate
227 // implementation (the correct 'construct()' overload)
229 explicit optional_base ( Expr const& expr, Expr const* tag )
238 // No-throw (assuming T::~T() doesn't)
239 ~optional_base() { destroy() ; }
241 // Assigns from another optional<T> (deep-copies the rhs value)
242 void assign ( optional_base const& rhs )
244 if (is_initialized())
246 if ( rhs.is_initialized() )
247 assign_value(rhs.get_impl(), is_reference_predicate() );
252 if ( rhs.is_initialized() )
253 construct(rhs.get_impl());
257 // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
259 void assign ( optional<U> const& rhs )
261 if (is_initialized())
263 if ( rhs.is_initialized() )
264 assign_value(static_cast<value_type>(rhs.get()), is_reference_predicate() );
269 if ( rhs.is_initialized() )
270 construct(static_cast<value_type>(rhs.get()));
274 // Assigns from a T (deep-copies the rhs value)
275 void assign ( argument_type val )
277 if (is_initialized())
278 assign_value(val, is_reference_predicate() );
282 // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
283 // No-throw (assuming T::~T() doesn't)
284 void assign ( none_t ) { destroy(); }
286 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
288 void assign_expr ( Expr const& expr, Expr const* tag )
290 if (is_initialized())
291 assign_expr_to_initialized(expr,tag);
292 else construct(expr,tag);
298 // Destroys the current value, if any, leaving this UNINITIALIZED
299 // No-throw (assuming T::~T() doesn't)
300 void reset() { destroy(); }
302 // Replaces the current value -if any- with 'val'
303 void reset ( argument_type val ) { assign(val); }
305 // Returns a pointer to the value if this is initialized, otherwise,
308 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
309 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
311 bool is_initialized() const { return m_initialized ; }
315 void construct ( argument_type val )
317 new (m_storage.address()) internal_type(val) ;
318 m_initialized = true ;
321 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
322 // Constructs in-place using the given factory
324 void construct ( Expr const& factory, in_place_factory_base const* )
326 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
327 boost_optional_detail::construct<value_type>(factory, m_storage.address());
328 m_initialized = true ;
331 // Constructs in-place using the given typed factory
333 void construct ( Expr const& factory, typed_in_place_factory_base const* )
335 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
336 factory.apply(m_storage.address()) ;
337 m_initialized = true ;
341 void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
344 construct(factory,tag);
347 // Constructs in-place using the given typed factory
349 void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
352 construct(factory,tag);
356 // Constructs using any expression implicitely convertible to the single argument
357 // of a one-argument T constructor.
358 // Converting constructions of optional<T> from optional<U> uses this function with
359 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
361 void construct ( Expr const& expr, void const* )
363 new (m_storage.address()) internal_type(expr) ;
364 m_initialized = true ;
367 // Assigns using a form any expression implicitely convertible to the single argument
368 // of a T's assignment operator.
369 // Converting assignments of optional<T> from optional<U> uses this function with
370 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
372 void assign_expr_to_initialized ( Expr const& expr, void const* )
374 assign_value(expr, is_reference_predicate());
377 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
378 // BCB5.64 (and probably lower versions) workaround.
379 // The in-place factories are supported by means of catch-all constructors
380 // and assignment operators (the functions are parameterized in terms of
381 // an arbitrary 'Expr' type)
382 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
383 // to the 'Expr'-taking functions even though explicit overloads are present for them.
384 // Thus, the following overload is needed to properly handle the case when the 'lhs'
385 // is another optional.
387 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
388 // instead of choosing the wrong overload
390 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
392 void construct ( Expr const& expr, optional_tag const* )
394 if ( expr.is_initialized() )
396 // An exception can be thrown here.
397 // It it happens, THIS will be left uninitialized.
398 new (m_storage.address()) internal_type(expr.get()) ;
399 m_initialized = true ;
404 void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
405 void assign_value ( argument_type val, is_reference_tag ) { construct(val); }
410 destroy_impl(is_reference_predicate()) ;
413 unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
415 reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
416 reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
418 pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
419 pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
423 // internal_type can be either T or reference_content<T>
424 internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
425 internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
427 // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
428 reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
429 reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
430 reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
431 reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
433 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
434 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
436 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->T::~T() ; m_initialized = false ; }
439 void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
441 // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
442 // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
443 // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
444 pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
445 pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; }
446 pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; }
447 pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; }
450 storage_type m_storage ;
453 } // namespace optional_detail
456 class optional : public optional_detail::optional_base<T>
458 typedef optional_detail::optional_base<T> base ;
460 typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ;
464 typedef optional<T> this_type ;
466 typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
467 typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
468 typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
469 typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
470 typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
471 typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
473 // Creates an optional<T> uninitialized.
475 optional() : base() {}
477 // Creates an optional<T> uninitialized.
479 optional( none_t none_ ) : base(none_) {}
481 // Creates an optional<T> initialized with 'val'.
482 // Can throw if T::T(T const&) does
483 optional ( argument_type val ) : base(val) {}
485 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
486 // Can throw if T::T(T const&) does
487 optional ( bool cond, argument_type val ) : base(cond,val) {}
489 #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
490 // NOTE: MSVC needs templated versions first
492 // Creates a deep copy of another convertible optional<U>
493 // Requires a valid conversion from U to T.
494 // Can throw if T::T(U const&) does
496 explicit optional ( optional<U> const& rhs )
500 if ( rhs.is_initialized() )
501 this->construct(rhs.get());
505 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
506 // Creates an optional<T> with an expression which can be either
507 // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
508 // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
509 // (c) Any expression implicitely convertible to the single type
510 // of a one-argument T's constructor.
511 // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
512 // even though explicit overloads are present for these.
513 // Depending on the above some T ctor is called.
514 // Can throw is the resolved T ctor throws.
516 explicit optional ( Expr const& expr ) : base(expr,&expr) {}
519 // Creates a deep copy of another optional<T>
520 // Can throw if T::T(T const&) does
521 optional ( optional const& rhs ) : base(rhs) {}
523 // No-throw (assuming T::~T() doesn't)
526 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
527 // Assigns from an expression. See corresponding constructor.
528 // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
530 optional& operator= ( Expr expr )
532 this->assign_expr(expr,&expr);
538 #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
539 // Assigns from another convertible optional<U> (converts && deep-copies the rhs value)
540 // Requires a valid conversion from U to T.
541 // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
543 optional& operator= ( optional<U> const& rhs )
550 // Assigns from another optional<T> (deep-copies the rhs value)
551 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
552 // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
553 optional& operator= ( optional const& rhs )
555 this->assign( rhs ) ;
559 // Assigns from a T (deep-copies the rhs value)
560 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
561 optional& operator= ( argument_type val )
563 this->assign( val ) ;
567 // Assigns from a "none"
568 // Which destroys the current value, if any, leaving this UNINITIALIZED
569 // No-throw (assuming T::~T() doesn't)
570 optional& operator= ( none_t none_ )
572 this->assign( none_ ) ;
576 // Returns a reference to the value if this is initialized, otherwise,
577 // the behaviour is UNDEFINED
579 reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
580 reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
582 // Returns a copy of the value if this is initialized, 'v' otherwise
583 reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
584 reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
586 // Returns a pointer to the value if this is initialized, otherwise,
587 // the behaviour is UNDEFINED
589 pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
590 pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
592 // Returns a reference to the value if this is initialized, otherwise,
593 // the behaviour is UNDEFINED
595 reference_const_type operator *() const { return this->get() ; }
596 reference_type operator *() { return this->get() ; }
598 // implicit conversion to "bool"
600 operator unspecified_bool_type() const { return this->safe_bool() ; }
602 // This is provided for those compilers which don't like the conversion to bool
604 bool operator!() const { return !this->is_initialized() ; }
607 // Returns optional<T>(v)
610 optional<T> make_optional ( T const& v )
612 return optional<T>(v);
615 // Returns optional<T>(cond,v)
618 optional<T> make_optional ( bool cond, T const& v )
620 return optional<T>(cond,v);
623 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
627 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
628 get ( optional<T> const& opt )
635 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
636 get ( optional<T>& opt )
641 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
645 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
646 get ( optional<T> const* opt )
648 return opt->get_ptr() ;
653 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
654 get ( optional<T>* opt )
656 return opt->get_ptr() ;
659 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
663 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
664 get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
666 return opt.get_value_or(v) ;
671 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
672 get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
674 return opt.get_value_or(v) ;
677 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
681 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
682 get_pointer ( optional<T> const& opt )
684 return opt.get_ptr() ;
689 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
690 get_pointer ( optional<T>& opt )
692 return opt.get_ptr() ;
695 // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
696 // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
700 // optional<T> vs optional<T> cases
705 bool operator == ( optional<T> const& x, optional<T> const& y )
706 { return equal_pointees(x,y); }
710 bool operator < ( optional<T> const& x, optional<T> const& y )
711 { return less_pointees(x,y); }
715 bool operator != ( optional<T> const& x, optional<T> const& y )
716 { return !( x == y ) ; }
720 bool operator > ( optional<T> const& x, optional<T> const& y )
725 bool operator <= ( optional<T> const& x, optional<T> const& y )
726 { return !( y < x ) ; }
730 bool operator >= ( optional<T> const& x, optional<T> const& y )
731 { return !( x < y ) ; }
735 // optional<T> vs T cases
739 bool operator == ( optional<T> const& x, T const& y )
740 { return equal_pointees(x, optional<T>(y)); }
744 bool operator < ( optional<T> const& x, T const& y )
745 { return less_pointees(x, optional<T>(y)); }
749 bool operator != ( optional<T> const& x, T const& y )
750 { return !( x == y ) ; }
754 bool operator > ( optional<T> const& x, T const& y )
759 bool operator <= ( optional<T> const& x, T const& y )
760 { return !( y < x ) ; }
764 bool operator >= ( optional<T> const& x, T const& y )
765 { return !( x < y ) ; }
768 // T vs optional<T> cases
773 bool operator == ( T const& x, optional<T> const& y )
774 { return equal_pointees( optional<T>(x), y ); }
778 bool operator < ( T const& x, optional<T> const& y )
779 { return less_pointees( optional<T>(x), y ); }
783 bool operator != ( T const& x, optional<T> const& y )
784 { return !( x == y ) ; }
788 bool operator > ( T const& x, optional<T> const& y )
793 bool operator <= ( T const& x, optional<T> const& y )
794 { return !( y < x ) ; }
798 bool operator >= ( T const& x, optional<T> const& y )
799 { return !( x < y ) ; }
803 // optional<T> vs none cases
808 bool operator == ( optional<T> const& x, none_t )
809 { return equal_pointees(x, optional<T>() ); }
813 bool operator < ( optional<T> const& x, none_t )
814 { return less_pointees(x,optional<T>() ); }
818 bool operator != ( optional<T> const& x, none_t y )
819 { return !( x == y ) ; }
823 bool operator > ( optional<T> const& x, none_t y )
828 bool operator <= ( optional<T> const& x, none_t y )
829 { return !( y < x ) ; }
833 bool operator >= ( optional<T> const& x, none_t y )
834 { return !( x < y ) ; }
837 // none vs optional<T> cases
842 bool operator == ( none_t x, optional<T> const& y )
843 { return equal_pointees(optional<T>() ,y); }
847 bool operator < ( none_t x, optional<T> const& y )
848 { return less_pointees(optional<T>() ,y); }
852 bool operator != ( none_t x, optional<T> const& y )
853 { return !( x == y ) ; }
857 bool operator > ( none_t x, optional<T> const& y )
862 bool operator <= ( none_t x, optional<T> const& y )
863 { return !( y < x ) ; }
867 bool operator >= ( none_t x, optional<T> const& y )
868 { return !( x < y ) ; }
871 // The following swap implementation follows the GCC workaround as found in
872 // "boost/detail/compressed_pair.hpp"
874 namespace optional_detail {
876 // GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)
877 #if BOOST_WORKAROUND(__GNUC__, < 3) \
878 || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2
880 #define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
884 // If both are initialized, calls swap(T&, T&). If this swap throws, both will remain initialized but their values are now unspecified.
885 // If only one is initialized, calls U.reset(*I), THEN I.reset().
886 // If U.reset(*I) throws, both are left UNCHANGED (U is kept uinitialized and I is never reset)
887 // If both are uninitialized, do nothing (no-throw)
890 void optional_swap ( optional<T>& x, optional<T>& y )
897 else if ( !!x && !y )
902 else if ( !!x && !!y )
904 // GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
905 #ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
906 // allow for Koenig lookup
913 } // namespace optional_detail
915 template<class T> inline void swap ( optional<T>& x, optional<T>& y )
917 optional_detail::optional_swap(x,y);