1 // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
2 // Copyright (C) 2014, 2015 Andrzej Krzemienski.
4 // Use, modification, and distribution is subject to the Boost Software
5 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/optional for documentation.
10 // You are welcome to contact the author at:
11 // fernando_cacciola@hotmail.com
14 // 27 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen
15 // 05 May 2014 (Added move semantics) Andrzej Krzemienski
17 #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
18 #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
24 #include <boost/config.hpp>
25 #include <boost/assert.hpp>
26 #include <boost/core/explicit_operator_bool.hpp>
27 #include <boost/optional/bad_optional_access.hpp>
28 #include <boost/static_assert.hpp>
29 #include <boost/throw_exception.hpp>
30 #include <boost/type.hpp>
31 #include <boost/type_traits/alignment_of.hpp>
32 #include <boost/type_traits/has_nothrow_constructor.hpp>
33 #include <boost/type_traits/type_with_alignment.hpp>
34 #include <boost/type_traits/remove_const.hpp>
35 #include <boost/type_traits/remove_reference.hpp>
36 #include <boost/type_traits/decay.hpp>
37 #include <boost/type_traits/is_base_of.hpp>
38 #include <boost/type_traits/is_lvalue_reference.hpp>
39 #include <boost/type_traits/is_nothrow_move_assignable.hpp>
40 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
41 #include <boost/type_traits/is_reference.hpp>
42 #include <boost/type_traits/is_rvalue_reference.hpp>
43 #include <boost/type_traits/is_same.hpp>
44 #include <boost/mpl/if.hpp>
45 #include <boost/mpl/bool.hpp>
46 #include <boost/mpl/not.hpp>
47 #include <boost/detail/reference_content.hpp>
48 #include <boost/move/utility.hpp>
49 #include <boost/none.hpp>
50 #include <boost/utility/addressof.hpp>
51 #include <boost/utility/compare_pointees.hpp>
52 #include <boost/utility/enable_if.hpp>
53 #include <boost/utility/in_place_factory.hpp>
54 #include <boost/utility/swap.hpp>
58 #include <boost/optional/optional_fwd.hpp>
60 #if (defined BOOST_NO_CXX11_RVALUE_REFERENCES) || (defined BOOST_OPTIONAL_CONFIG_NO_RVALUE_REFERENCES)
61 #define BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
64 #if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION,<=700)
65 // AFAICT only Intel 7 correctly resolves the overload set
66 // that includes the in-place factory taking functions,
67 // so for the other icc versions, in-place factory support
69 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
72 #if BOOST_WORKAROUND(__BORLANDC__, <= 0x551)
73 // BCB (5.5.1) cannot parse the nested template struct in an inplace factory.
74 #define BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
77 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) \
78 && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581) )
79 // BCB (up to 5.64) has the following bug:
80 // If there is a member function/operator template of the form
81 // template<class Expr> mfunc( Expr expr ) ;
82 // some calls are resolved to this even if there are other better matches.
83 // The effect of this bug is that calls to converting ctors and assignments
84 // are incrorrectly sink to this general catch-all member function template as shown above.
85 #define BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
88 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
89 // GCC since 3.3 has may_alias attribute that helps to alleviate optimizer issues with
90 // regard to violation of the strict aliasing rules. The optional< T > storage type is marked
91 // with this attribute in order to let the compiler know that it will alias objects of type T
92 // and silence compilation warnings.
93 #define BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS
96 // Daniel Wallin discovered that bind/apply.hpp badly interacts with the apply<>
97 // member template of a factory as used in the optional<> implementation.
98 // He proposed this simple fix which is to move the call to apply<> outside
100 namespace boost_optional_detail
102 template <class T, class Factory>
103 inline void construct(Factory const& factory, void* address)
105 factory.BOOST_NESTED_TEMPLATE apply<T>(address);
112 class in_place_factory_base ;
113 class typed_in_place_factory_base ;
115 // This forward is needed to refer to namespace scope swap from the member swap
116 template<class T> void swap ( optional<T>& x, optional<T>& y );
118 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 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
156 typedef T && rval_reference_type ;
157 typedef T && reference_type_of_temporary_wrapper;
158 #ifdef BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES
159 // GCC 4.4 has support for an early draft of rvalue references. The conforming version below
160 // causes warnings about returning references to a temporary.
161 static T&& move(T&& r) { return r; }
163 static rval_reference_type move(reference_type r) { return boost::move(r); }
166 typedef T const* pointer_const_type ;
167 typedef T * pointer_type ;
168 typedef T const& argument_type ;
172 struct types_when_is_ref
174 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
176 typedef raw_type& reference_const_type ;
177 typedef raw_type& reference_type ;
178 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
179 typedef BOOST_DEDUCED_TYPENAME remove_const<raw_type>::type&& rval_reference_type ;
180 typedef raw_type& reference_type_of_temporary_wrapper;
181 static reference_type move(reference_type r) { return r; }
183 typedef raw_type* pointer_const_type ;
184 typedef raw_type* pointer_type ;
185 typedef raw_type& argument_type ;
188 template <class To, class From>
189 void prevent_binding_rvalue_ref_to_optional_lvalue_ref()
191 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
192 BOOST_STATIC_ASSERT_MSG(
193 !boost::is_lvalue_reference<To>::value || !boost::is_rvalue_reference<From>::value,
194 "binding rvalue references to optional lvalue references is disallowed");
198 struct optional_tag {} ;
201 class optional_base : public optional_tag
206 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
207 BOOST_DEDUCED_TYPENAME
209 ::boost::detail::make_reference_content<T>::type internal_type ;
211 typedef aligned_storage<internal_type> storage_type ;
213 typedef types_when_isnt_ref<T> types_when_not_ref ;
214 typedef types_when_is_ref<T> types_when_ref ;
216 typedef optional_base<T> this_type ;
220 typedef T value_type ;
222 typedef mpl::true_ is_reference_tag ;
223 typedef mpl::false_ is_not_reference_tag ;
225 typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
228 typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
231 typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
232 typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
233 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
234 typedef BOOST_DEDUCED_TYPENAME types::rval_reference_type rval_reference_type ;
235 typedef BOOST_DEDUCED_TYPENAME types::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
237 typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
238 typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
239 typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
241 // Creates an optional<T> uninitialized.
245 m_initialized(false) {}
247 // Creates an optional<T> uninitialized.
249 optional_base ( none_t )
251 m_initialized(false) {}
253 // Creates an optional<T> initialized with 'val'.
254 // Can throw if T::T(T const&) does
255 optional_base ( argument_type val )
262 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
263 // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
264 // Can throw if T::T(T&&) does
265 optional_base ( rval_reference_type val )
269 construct( boost::move(val) );
273 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional<T>.
274 // Can throw if T::T(T const&) does
275 optional_base ( bool cond, argument_type val )
283 // Creates a deep copy of another optional<T>
284 // Can throw if T::T(T const&) does
285 optional_base ( optional_base const& rhs )
289 if ( rhs.is_initialized() )
290 construct(rhs.get_impl());
293 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
294 // Creates a deep move of another optional<T>
295 // Can throw if T::T(T&&) does
296 optional_base ( optional_base&& rhs )
300 if ( rhs.is_initialized() )
301 construct( boost::move(rhs.get_impl()) );
305 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
307 template<class Expr, class PtrExpr>
308 explicit optional_base ( Expr&& expr, PtrExpr const* tag )
312 construct(boost::forward<Expr>(expr),tag);
316 // This is used for both converting and in-place constructions.
317 // Derived classes use the 'tag' to select the appropriate
318 // implementation (the correct 'construct()' overload)
320 explicit optional_base ( Expr const& expr, Expr const* tag )
330 // No-throw (assuming T::~T() doesn't)
331 ~optional_base() { destroy() ; }
333 // Assigns from another optional<T> (deep-copies the rhs value)
334 void assign ( optional_base const& rhs )
336 if (is_initialized())
338 if ( rhs.is_initialized() )
339 assign_value(rhs.get_impl(), is_reference_predicate() );
344 if ( rhs.is_initialized() )
345 construct(rhs.get_impl());
349 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
350 // Assigns from another optional<T> (deep-moves the rhs value)
351 void assign ( optional_base&& rhs )
353 if (is_initialized())
355 if ( rhs.is_initialized() )
356 assign_value(boost::move(rhs.get_impl()), is_reference_predicate() );
361 if ( rhs.is_initialized() )
362 construct(boost::move(rhs.get_impl()));
367 // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
369 void assign ( optional<U> const& rhs )
371 if (is_initialized())
373 if ( rhs.is_initialized() )
374 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
375 assign_value(rhs.get(), is_reference_predicate() );
377 assign_value(static_cast<value_type>(rhs.get()), is_reference_predicate() );
384 if ( rhs.is_initialized() )
385 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
386 construct(rhs.get());
388 construct(static_cast<value_type>(rhs.get()));
393 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
394 // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
396 void assign ( optional<U>&& rhs )
398 typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
399 if (is_initialized())
401 if ( rhs.is_initialized() )
402 assign_value(static_cast<ref_type>(rhs.get()), is_reference_predicate() );
407 if ( rhs.is_initialized() )
408 construct(static_cast<ref_type>(rhs.get()));
413 // Assigns from a T (deep-copies the rhs value)
414 void assign ( argument_type val )
416 if (is_initialized())
417 assign_value(val, is_reference_predicate() );
421 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
422 // Assigns from a T (deep-moves the rhs value)
423 void assign ( rval_reference_type val )
425 if (is_initialized())
426 assign_value( boost::move(val), is_reference_predicate() );
427 else construct( boost::move(val) );
431 // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
432 // No-throw (assuming T::~T() doesn't)
433 void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
435 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
437 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
438 template<class Expr, class ExprPtr>
439 void assign_expr ( Expr&& expr, ExprPtr const* tag )
441 if (is_initialized())
442 assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
443 else construct(boost::forward<Expr>(expr),tag);
447 void assign_expr ( Expr const& expr, Expr const* tag )
449 if (is_initialized())
450 assign_expr_to_initialized(expr,tag);
451 else construct(expr,tag);
459 // **DEPPRECATED** Destroys the current value, if any, leaving this UNINITIALIZED
460 // No-throw (assuming T::~T() doesn't)
461 void reset() BOOST_NOEXCEPT { destroy(); }
463 // **DEPPRECATED** Replaces the current value -if any- with 'val'
464 void reset ( argument_type val ) { assign(val); }
466 // Returns a pointer to the value if this is initialized, otherwise,
469 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
470 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
472 bool is_initialized() const { return m_initialized ; }
476 void construct ( argument_type val )
478 ::new (m_storage.address()) internal_type(val) ;
479 m_initialized = true ;
482 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
483 void construct ( rval_reference_type val )
485 ::new (m_storage.address()) internal_type( types::move(val) ) ;
486 m_initialized = true ;
491 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
492 // Constructs in-place
493 // upon exception *this is always uninitialized
494 template<class... Args>
495 void emplace_assign ( Args&&... args )
498 ::new (m_storage.address()) internal_type( boost::forward<Args>(args)... );
499 m_initialized = true ;
501 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
503 void emplace_assign ( Arg&& arg )
506 ::new (m_storage.address()) internal_type( boost::forward<Arg>(arg) );
507 m_initialized = true ;
511 void emplace_assign ( const Arg& arg )
514 ::new (m_storage.address()) internal_type( arg );
515 m_initialized = true ;
519 void emplace_assign ( Arg& arg )
522 ::new (m_storage.address()) internal_type( arg );
523 m_initialized = true ;
527 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
529 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
530 // Constructs in-place using the given factory
532 void construct ( Expr&& factory, in_place_factory_base const* )
534 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
535 boost_optional_detail::construct<value_type>(factory, m_storage.address());
536 m_initialized = true ;
539 // Constructs in-place using the given typed factory
541 void construct ( Expr&& factory, typed_in_place_factory_base const* )
543 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
544 factory.apply(m_storage.address()) ;
545 m_initialized = true ;
549 void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
552 construct(factory,tag);
555 // Constructs in-place using the given typed factory
557 void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
560 construct(factory,tag);
564 // Constructs in-place using the given factory
566 void construct ( Expr const& factory, in_place_factory_base const* )
568 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
569 boost_optional_detail::construct<value_type>(factory, m_storage.address());
570 m_initialized = true ;
573 // Constructs in-place using the given typed factory
575 void construct ( Expr const& factory, typed_in_place_factory_base const* )
577 BOOST_STATIC_ASSERT ( ::boost::mpl::not_<is_reference_predicate>::value ) ;
578 factory.apply(m_storage.address()) ;
579 m_initialized = true ;
583 void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
586 construct(factory,tag);
589 // Constructs in-place using the given typed factory
591 void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
594 construct(factory,tag);
600 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
601 // Constructs using any expression implicitly convertible to the single argument
602 // of a one-argument T constructor.
603 // Converting constructions of optional<T> from optional<U> uses this function with
604 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
606 void construct ( Expr&& expr, void const* )
608 new (m_storage.address()) internal_type(boost::forward<Expr>(expr)) ;
609 m_initialized = true ;
612 // Assigns using a form any expression implicitly convertible to the single argument
613 // of a T's assignment operator.
614 // Converting assignments of optional<T> from optional<U> uses this function with
615 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
617 void assign_expr_to_initialized ( Expr&& expr, void const* )
619 assign_value(boost::forward<Expr>(expr), is_reference_predicate());
622 // Constructs using any expression implicitly convertible to the single argument
623 // of a one-argument T constructor.
624 // Converting constructions of optional<T> from optional<U> uses this function with
625 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
627 void construct ( Expr const& expr, void const* )
629 new (m_storage.address()) internal_type(expr) ;
630 m_initialized = true ;
633 // Assigns using a form any expression implicitly convertible to the single argument
634 // of a T's assignment operator.
635 // Converting assignments of optional<T> from optional<U> uses this function with
636 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
638 void assign_expr_to_initialized ( Expr const& expr, void const* )
640 assign_value(expr, is_reference_predicate());
645 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
646 // BCB5.64 (and probably lower versions) workaround.
647 // The in-place factories are supported by means of catch-all constructors
648 // and assignment operators (the functions are parameterized in terms of
649 // an arbitrary 'Expr' type)
650 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
651 // to the 'Expr'-taking functions even though explicit overloads are present for them.
652 // Thus, the following overload is needed to properly handle the case when the 'lhs'
653 // is another optional.
655 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
656 // instead of choosing the wrong overload
658 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
659 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
661 void construct ( Expr&& expr, optional_tag const* )
663 if ( expr.is_initialized() )
665 // An exception can be thrown here.
666 // It it happens, THIS will be left uninitialized.
667 new (m_storage.address()) internal_type(types::move(expr.get())) ;
668 m_initialized = true ;
672 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
674 void construct ( Expr const& expr, optional_tag const* )
676 if ( expr.is_initialized() )
678 // An exception can be thrown here.
679 // It it happens, THIS will be left uninitialized.
680 new (m_storage.address()) internal_type(expr.get()) ;
681 m_initialized = true ;
685 #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
687 void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
688 void assign_value ( argument_type val, is_reference_tag ) { construct(val); }
689 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
690 void assign_value ( rval_reference_type val, is_not_reference_tag ) { get_impl() = static_cast<rval_reference_type>(val); }
691 void assign_value ( rval_reference_type val, is_reference_tag ) { construct( static_cast<rval_reference_type>(val) ); }
697 destroy_impl(is_reference_predicate()) ;
700 reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
701 reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
703 pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
704 pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
708 // internal_type can be either T or reference_content<T>
709 #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS)
710 // This workaround is supposed to silence GCC warnings about broken strict aliasing rules
711 internal_type const* get_object() const
713 union { void const* ap_pvoid; internal_type const* as_ptype; } caster = { m_storage.address() };
714 return caster.as_ptype;
716 internal_type * get_object()
718 union { void* ap_pvoid; internal_type* as_ptype; } caster = { m_storage.address() };
719 return caster.as_ptype;
722 internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
723 internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
726 // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
727 reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
728 reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
729 reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
730 reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
732 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
733 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
735 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->~T() ; m_initialized = false ; }
738 void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
740 // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
741 // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
742 // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
743 pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
744 pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; }
745 pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; }
746 pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; }
749 storage_type m_storage ;
752 } // namespace optional_detail
755 class optional : public optional_detail::optional_base<T>
757 typedef optional_detail::optional_base<T> base ;
761 typedef optional<T> this_type ;
763 typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
764 typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
765 typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
766 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
767 typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
768 typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
770 typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
771 typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
772 typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
774 // Creates an optional<T> uninitialized.
776 optional() BOOST_NOEXCEPT : base() {}
778 // Creates an optional<T> uninitialized.
780 optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
782 // Creates an optional<T> initialized with 'val'.
783 // Can throw if T::T(T const&) does
784 optional ( argument_type val ) : base(val) {}
786 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
787 // Creates an optional<T> initialized with 'move(val)'.
788 // Can throw if T::T(T &&) does
789 optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
790 {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, rval_reference_type>();}
793 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
794 // Can throw if T::T(T const&) does
795 optional ( bool cond, argument_type val ) : base(cond,val) {}
797 // NOTE: MSVC needs templated versions first
799 // Creates a deep copy of another convertible optional<U>
800 // Requires a valid conversion from U to T.
801 // Can throw if T::T(U const&) does
803 explicit optional ( optional<U> const& rhs )
807 if ( rhs.is_initialized() )
808 this->construct(rhs.get());
811 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
812 // Creates a deep move of another convertible optional<U>
813 // Requires a valid conversion from U to T.
814 // Can throw if T::T(U&&) does
816 explicit optional ( optional<U> && rhs )
820 if ( rhs.is_initialized() )
821 this->construct( boost::move(rhs.get()) );
825 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
826 // Creates an optional<T> with an expression which can be either
827 // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
828 // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
829 // (c) Any expression implicitly convertible to the single type
830 // of a one-argument T's constructor.
831 // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
832 // even though explicit overloads are present for these.
833 // Depending on the above some T ctor is called.
834 // Can throw if the resolved T ctor throws.
835 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
839 explicit optional ( Expr&& expr,
840 BOOST_DEDUCED_TYPENAME boost::disable_if_c<
841 (boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type>::value) ||
842 boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type, none_t>::value >::type* = 0
844 : base(boost::forward<Expr>(expr),boost::addressof(expr))
845 {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, Expr&&>();}
849 explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
850 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
851 #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
853 // Creates a deep copy of another optional<T>
854 // Can throw if T::T(T const&) does
855 optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
857 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
858 // Creates a deep move of another optional<T>
859 // Can throw if T::T(T&&) does
860 optional ( optional && rhs )
861 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
862 : base( boost::move(rhs) )
866 // No-throw (assuming T::~T() doesn't)
869 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
870 // Assigns from an expression. See corresponding constructor.
871 // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
872 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
875 BOOST_DEDUCED_TYPENAME boost::disable_if_c<
876 boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type>::value ||
877 boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type, none_t>::value,
880 operator= ( Expr&& expr )
882 optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, Expr&&>();
883 this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
889 optional& operator= ( Expr const& expr )
891 this->assign_expr(expr,boost::addressof(expr));
894 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
895 #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
897 // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
898 // Requires a valid conversion from U to T.
899 // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
901 optional& operator= ( optional<U> const& rhs )
907 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
908 // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
909 // Requires a valid conversion from U to T.
910 // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
912 optional& operator= ( optional<U> && rhs )
914 this->assign(boost::move(rhs));
919 // Assigns from another optional<T> (deep-copies the rhs value)
920 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
921 // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
922 optional& operator= ( optional const& rhs )
924 this->assign( static_cast<base const&>(rhs) ) ;
928 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
929 // Assigns from another optional<T> (deep-moves the rhs value)
930 optional& operator= ( optional && rhs )
931 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
933 this->assign( static_cast<base &&>(rhs) ) ;
938 // Assigns from a T (deep-copies the rhs value)
939 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
940 optional& operator= ( argument_type val )
942 this->assign( val ) ;
946 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
947 // Assigns from a T (deep-moves the rhs value)
948 optional& operator= ( rval_reference_type val )
950 optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, rval_reference_type>();
951 this->assign( boost::move(val) ) ;
956 // Assigns from a "none"
957 // Which destroys the current value, if any, leaving this UNINITIALIZED
958 // No-throw (assuming T::~T() doesn't)
959 optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
961 this->assign( none_ ) ;
965 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
966 // Constructs in-place
967 // upon exception *this is always uninitialized
968 template<class... Args>
969 void emplace ( Args&&... args )
971 this->emplace_assign( boost::forward<Args>(args)... );
973 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
975 void emplace ( Arg&& arg )
977 this->emplace_assign( boost::forward<Arg>(arg) );
981 void emplace ( const Arg& arg )
983 this->emplace_assign( arg );
987 void emplace ( Arg& arg )
989 this->emplace_assign( arg );
993 void swap( optional & arg )
994 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
996 // allow for Koenig lookup
997 boost::swap(*this, arg);
1001 // Returns a reference to the value if this is initialized, otherwise,
1002 // the behaviour is UNDEFINED
1004 reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
1005 reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
1007 // Returns a copy of the value if this is initialized, 'v' otherwise
1008 reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
1009 reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
1011 // Returns a pointer to the value if this is initialized, otherwise,
1012 // the behaviour is UNDEFINED
1014 pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
1015 pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
1017 // Returns a reference to the value if this is initialized, otherwise,
1018 // the behaviour is UNDEFINED
1020 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
1021 reference_const_type operator *() const& { return this->get() ; }
1022 reference_type operator *() & { return this->get() ; }
1023 reference_type_of_temporary_wrapper operator *() && { return base::types::move(this->get()) ; }
1025 reference_const_type operator *() const { return this->get() ; }
1026 reference_type operator *() { return this->get() ; }
1027 #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
1029 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
1030 reference_const_type value() const&
1032 if (this->is_initialized())
1033 return this->get() ;
1035 throw_exception(bad_optional_access());
1038 reference_type value() &
1040 if (this->is_initialized())
1041 return this->get() ;
1043 throw_exception(bad_optional_access());
1046 reference_type_of_temporary_wrapper value() &&
1048 if (this->is_initialized())
1049 return base::types::move(this->get()) ;
1051 throw_exception(bad_optional_access());
1055 reference_const_type value() const
1057 if (this->is_initialized())
1058 return this->get() ;
1060 throw_exception(bad_optional_access());
1063 reference_type value()
1065 if (this->is_initialized())
1066 return this->get() ;
1068 throw_exception(bad_optional_access());
1073 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
1075 value_type value_or ( U&& v ) const&
1077 if (this->is_initialized())
1080 return boost::forward<U>(v);
1084 value_type value_or ( U&& v ) &&
1086 if (this->is_initialized())
1087 return base::types::move(get());
1089 return boost::forward<U>(v);
1091 #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1093 value_type value_or ( U&& v ) const
1095 if (this->is_initialized())
1098 return boost::forward<U>(v);
1102 value_type value_or ( U const& v ) const
1104 if (this->is_initialized())
1111 value_type value_or ( U& v ) const
1113 if (this->is_initialized())
1121 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
1122 template <typename F>
1123 value_type value_or_eval ( F f ) const&
1125 if (this->is_initialized())
1131 template <typename F>
1132 value_type value_or_eval ( F f ) &&
1134 if (this->is_initialized())
1135 return base::types::move(get());
1140 template <typename F>
1141 value_type value_or_eval ( F f ) const
1143 if (this->is_initialized())
1150 bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
1152 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
1155 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1159 BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal.");
1163 // Returns optional<T>(v)
1166 optional<T> make_optional ( T const& v )
1168 return optional<T>(v);
1171 // Returns optional<T>(cond,v)
1174 optional<T> make_optional ( bool cond, T const& v )
1176 return optional<T>(cond,v);
1179 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1183 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
1184 get ( optional<T> const& opt )
1191 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1192 get ( optional<T>& opt )
1197 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1201 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1202 get ( optional<T> const* opt )
1204 return opt->get_ptr() ;
1209 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1210 get ( optional<T>* opt )
1212 return opt->get_ptr() ;
1215 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1219 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
1220 get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
1222 return opt.get_value_or(v) ;
1227 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1228 get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
1230 return opt.get_value_or(v) ;
1233 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1237 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1238 get_pointer ( optional<T> const& opt )
1240 return opt.get_ptr() ;
1245 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1246 get_pointer ( optional<T>& opt )
1248 return opt.get_ptr() ;
1251 // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
1252 template<class CharType, class CharTrait>
1253 std::basic_ostream<CharType, CharTrait>&
1254 operator<<(std::basic_ostream<CharType, CharTrait>& out, optional_detail::optional_tag const& v)
1256 BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
1259 // optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).
1260 // WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.
1264 // optional<T> vs optional<T> cases
1269 bool operator == ( optional<T> const& x, optional<T> const& y )
1270 { return equal_pointees(x,y); }
1274 bool operator < ( optional<T> const& x, optional<T> const& y )
1275 { return less_pointees(x,y); }
1279 bool operator != ( optional<T> const& x, optional<T> const& y )
1280 { return !( x == y ) ; }
1284 bool operator > ( optional<T> const& x, optional<T> const& y )
1289 bool operator <= ( optional<T> const& x, optional<T> const& y )
1290 { return !( y < x ) ; }
1294 bool operator >= ( optional<T> const& x, optional<T> const& y )
1295 { return !( x < y ) ; }
1299 // optional<T> vs T cases
1303 bool operator == ( optional<T> const& x, T const& y )
1304 { return equal_pointees(x, optional<T>(y)); }
1308 bool operator < ( optional<T> const& x, T const& y )
1309 { return less_pointees(x, optional<T>(y)); }
1313 bool operator != ( optional<T> const& x, T const& y )
1314 { return !( x == y ) ; }
1318 bool operator > ( optional<T> const& x, T const& y )
1323 bool operator <= ( optional<T> const& x, T const& y )
1324 { return !( y < x ) ; }
1328 bool operator >= ( optional<T> const& x, T const& y )
1329 { return !( x < y ) ; }
1332 // T vs optional<T> cases
1337 bool operator == ( T const& x, optional<T> const& y )
1338 { return equal_pointees( optional<T>(x), y ); }
1342 bool operator < ( T const& x, optional<T> const& y )
1343 { return less_pointees( optional<T>(x), y ); }
1347 bool operator != ( T const& x, optional<T> const& y )
1348 { return !( x == y ) ; }
1352 bool operator > ( T const& x, optional<T> const& y )
1357 bool operator <= ( T const& x, optional<T> const& y )
1358 { return !( y < x ) ; }
1362 bool operator >= ( T const& x, optional<T> const& y )
1363 { return !( x < y ) ; }
1367 // optional<T> vs none cases
1372 bool operator == ( optional<T> const& x, none_t ) BOOST_NOEXCEPT
1377 bool operator < ( optional<T> const& x, none_t )
1378 { return less_pointees(x,optional<T>() ); }
1382 bool operator != ( optional<T> const& x, none_t ) BOOST_NOEXCEPT
1387 bool operator > ( optional<T> const& x, none_t y )
1392 bool operator <= ( optional<T> const& x, none_t y )
1393 { return !( y < x ) ; }
1397 bool operator >= ( optional<T> const& x, none_t y )
1398 { return !( x < y ) ; }
1401 // none vs optional<T> cases
1406 bool operator == ( none_t , optional<T> const& y ) BOOST_NOEXCEPT
1411 bool operator < ( none_t , optional<T> const& y )
1412 { return less_pointees(optional<T>() ,y); }
1416 bool operator != ( none_t, optional<T> const& y ) BOOST_NOEXCEPT
1421 bool operator > ( none_t x, optional<T> const& y )
1426 bool operator <= ( none_t x, optional<T> const& y )
1427 { return !( y < x ) ; }
1431 bool operator >= ( none_t x, optional<T> const& y )
1432 { return !( x < y ) ; }
1434 namespace optional_detail {
1436 template<bool use_default_constructor> struct swap_selector;
1439 struct swap_selector<true>
1442 static void optional_swap ( optional<T>& x, optional<T>& y )
1444 const bool hasX = !!x;
1445 const bool hasY = !!y;
1447 if ( !hasX && !hasY )
1451 x = boost::in_place();
1453 y = boost::in_place();
1455 // Boost.Utility.Swap will take care of ADL and workarounds for broken compilers
1456 boost::swap(x.get(),y.get());
1465 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1467 struct swap_selector<false>
1470 static void optional_swap ( optional<T>& x, optional<T>& y )
1471 //BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && BOOST_NOEXCEPT_EXPR(boost::swap(*x, *y)))
1477 boost::swap(*x, *y);
1481 y = boost::move(*x);
1489 x = boost::move(*y);
1497 struct swap_selector<false>
1500 static void optional_swap ( optional<T>& x, optional<T>& y )
1502 const bool hasX = !!x;
1503 const bool hasY = !!y;
1505 if ( !hasX && hasY )
1510 else if ( hasX && !hasY )
1515 else if ( hasX && hasY )
1517 // Boost.Utility.Swap will take care of ADL and workarounds for broken compilers
1518 boost::swap(x.get(),y.get());
1522 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1524 } // namespace optional_detail
1527 struct optional_swap_should_use_default_constructor : has_nothrow_default_constructor<T> {} ;
1529 template<class T> inline void swap ( optional<T>& x, optional<T>& y )
1530 //BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && BOOST_NOEXCEPT_EXPR(boost::swap(*x, *y)))
1532 optional_detail::swap_selector<optional_swap_should_use_default_constructor<T>::value>::optional_swap(x, y);
1535 } // namespace boost