1 // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
2 // Copyright (C) 2014 - 2018 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
23 #ifdef BOOST_OPTIONAL_DETAIL_USE_STD_TYPE_TRAITS
24 # include <type_traits>
27 #include <boost/assert.hpp>
28 #include <boost/core/addressof.hpp>
29 #include <boost/core/enable_if.hpp>
30 #include <boost/core/explicit_operator_bool.hpp>
31 #include <boost/core/swap.hpp>
32 #include <boost/optional/bad_optional_access.hpp>
33 #include <boost/static_assert.hpp>
34 #include <boost/throw_exception.hpp>
35 #include <boost/type.hpp>
36 #include <boost/type_traits/alignment_of.hpp>
37 #include <boost/type_traits/conditional.hpp>
38 #include <boost/type_traits/has_nothrow_constructor.hpp>
39 #include <boost/type_traits/type_with_alignment.hpp>
40 #include <boost/type_traits/remove_const.hpp>
41 #include <boost/type_traits/remove_reference.hpp>
42 #include <boost/type_traits/decay.hpp>
43 #include <boost/type_traits/is_base_of.hpp>
44 #include <boost/type_traits/is_const.hpp>
45 #include <boost/type_traits/is_constructible.hpp>
46 #include <boost/type_traits/is_lvalue_reference.hpp>
47 #include <boost/type_traits/is_nothrow_move_assignable.hpp>
48 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
49 #include <boost/type_traits/is_rvalue_reference.hpp>
50 #include <boost/type_traits/is_same.hpp>
51 #include <boost/type_traits/is_volatile.hpp>
52 #include <boost/type_traits/is_scalar.hpp>
53 #include <boost/move/utility.hpp>
54 #include <boost/none.hpp>
55 #include <boost/utility/compare_pointees.hpp>
56 #include <boost/utility/result_of.hpp>
58 #include <boost/optional/optional_fwd.hpp>
59 #include <boost/optional/detail/optional_config.hpp>
60 #include <boost/optional/detail/optional_factory_support.hpp>
61 #include <boost/optional/detail/optional_aligned_storage.hpp>
63 namespace boost { namespace optional_detail {
66 struct optional_value_type
71 struct optional_value_type< ::boost::optional<T> >
76 }} // namespace boost::optional_detail
78 #ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
79 #include <boost/optional/detail/old_optional_implementation.hpp>
83 namespace optional_ns {
85 // a tag for in-place initialization of contained value
86 struct in_place_init_t
89 explicit in_place_init_t(init_tag){}
91 const in_place_init_t in_place_init ((in_place_init_t::init_tag()));
93 // a tag for conditional in-place initialization of contained value
94 struct in_place_init_if_t
97 explicit in_place_init_if_t(init_tag){}
99 const in_place_init_if_t in_place_init_if ((in_place_init_if_t::init_tag()));
101 } // namespace optional_ns
103 using optional_ns::in_place_init_t;
104 using optional_ns::in_place_init;
105 using optional_ns::in_place_init_if_t;
106 using optional_ns::in_place_init_if;
108 namespace optional_detail {
110 struct init_value_tag {};
112 struct optional_tag {};
116 class optional_base : public optional_tag
120 typedef aligned_storage<T> storage_type ;
121 typedef optional_base<T> this_type ;
125 typedef T value_type ;
128 typedef T & reference_type ;
129 typedef T const& reference_const_type ;
130 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
131 typedef T && rval_reference_type ;
132 typedef T && reference_type_of_temporary_wrapper ;
134 typedef T * pointer_type ;
135 typedef T const* pointer_const_type ;
136 typedef T const& argument_type ;
138 // Creates an optional<T> uninitialized.
142 m_initialized(false) {}
144 // Creates an optional<T> uninitialized.
146 optional_base ( none_t )
148 m_initialized(false) {}
150 // Creates an optional<T> initialized with 'val'.
151 // Can throw if T::T(T const&) does
152 optional_base ( init_value_tag, argument_type val )
159 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
160 // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
161 // Can throw if T::T(T&&) does
162 optional_base ( init_value_tag, rval_reference_type val )
166 construct( boost::move(val) );
170 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional<T>.
171 // Can throw if T::T(T const&) does
172 optional_base ( bool cond, argument_type val )
180 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
181 // Creates an optional<T> initialized with 'move(val)' IFF cond is true, otherwise creates an uninitialized optional<T>.
182 // Can throw if T::T(T &&) does
183 optional_base ( bool cond, rval_reference_type val )
188 construct(boost::move(val));
192 // Creates a deep copy of another optional<T>
193 // Can throw if T::T(T const&) does
194 optional_base ( optional_base const& rhs )
198 if ( rhs.is_initialized() )
199 construct(rhs.get_impl());
202 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
203 // Creates a deep move of another optional<T>
204 // Can throw if T::T(T&&) does
205 optional_base ( optional_base&& rhs )
206 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
210 if ( rhs.is_initialized() )
211 construct( boost::move(rhs.get_impl()) );
215 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
217 template<class Expr, class PtrExpr>
218 explicit optional_base ( Expr&& expr, PtrExpr const* tag )
222 construct(boost::forward<Expr>(expr),tag);
226 // This is used for both converting and in-place constructions.
227 // Derived classes use the 'tag' to select the appropriate
228 // implementation (the correct 'construct()' overload)
230 explicit optional_base ( Expr const& expr, Expr const* tag )
239 optional_base& operator= ( optional_base const& rhs )
245 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
246 optional_base& operator= ( optional_base && rhs )
247 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
249 this->assign(static_cast<optional_base&&>(rhs));
254 // No-throw (assuming T::~T() doesn't)
255 ~optional_base() { destroy() ; }
257 // Assigns from another optional<T> (deep-copies the rhs value)
258 void assign ( optional_base const& rhs )
260 if (is_initialized())
262 if ( rhs.is_initialized() )
263 assign_value(rhs.get_impl());
268 if ( rhs.is_initialized() )
269 construct(rhs.get_impl());
273 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
274 // Assigns from another optional<T> (deep-moves the rhs value)
275 void assign ( optional_base&& rhs )
277 if (is_initialized())
279 if ( rhs.is_initialized() )
280 assign_value( boost::move(rhs.get_impl()) );
285 if ( rhs.is_initialized() )
286 construct(boost::move(rhs.get_impl()));
291 // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
293 void assign ( optional<U> const& rhs )
295 if (is_initialized())
297 if ( rhs.is_initialized() )
298 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
299 assign_value( rhs.get() );
301 assign_value( static_cast<value_type>(rhs.get()) );
308 if ( rhs.is_initialized() )
309 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
310 construct(rhs.get());
312 construct(static_cast<value_type>(rhs.get()));
317 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
318 // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
320 void assign ( optional<U>&& rhs )
322 typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
323 if (is_initialized())
325 if ( rhs.is_initialized() )
326 assign_value( static_cast<ref_type>(rhs.get()) );
331 if ( rhs.is_initialized() )
332 construct(static_cast<ref_type>(rhs.get()));
337 // Assigns from a T (deep-copies the rhs value)
338 void assign ( argument_type val )
340 if (is_initialized())
345 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
346 // Assigns from a T (deep-moves the rhs value)
347 void assign ( rval_reference_type val )
349 if (is_initialized())
350 assign_value( boost::move(val) );
351 else construct( boost::move(val) );
355 // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
356 // No-throw (assuming T::~T() doesn't)
357 void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
359 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
361 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
362 template<class Expr, class ExprPtr>
363 void assign_expr ( Expr&& expr, ExprPtr const* tag )
365 if (is_initialized())
366 assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
367 else construct(boost::forward<Expr>(expr),tag);
371 void assign_expr ( Expr const& expr, Expr const* tag )
373 if (is_initialized())
374 assign_expr_to_initialized(expr,tag);
375 else construct(expr,tag);
383 // Destroys the current value, if any, leaving this UNINITIALIZED
384 // No-throw (assuming T::~T() doesn't)
385 void reset() BOOST_NOEXCEPT { destroy(); }
387 // **DEPPRECATED** Replaces the current value -if any- with 'val'
388 void reset ( argument_type val ) { assign(val); }
390 // Returns a pointer to the value if this is initialized, otherwise,
393 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
394 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
396 bool is_initialized() const BOOST_NOEXCEPT { return m_initialized ; }
400 void construct ( argument_type val )
402 ::new (m_storage.address()) value_type(val) ;
403 m_initialized = true ;
406 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
407 void construct ( rval_reference_type val )
409 ::new (m_storage.address()) value_type( boost::move(val) ) ;
410 m_initialized = true ;
415 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
416 // Constructs in-place
417 // upon exception *this is always uninitialized
418 template<class... Args>
419 void construct ( in_place_init_t, Args&&... args )
421 ::new (m_storage.address()) value_type( boost::forward<Args>(args)... ) ;
422 m_initialized = true ;
425 template<class... Args>
426 void emplace_assign ( Args&&... args )
429 construct(in_place_init, boost::forward<Args>(args)...);
432 template<class... Args>
433 explicit optional_base ( in_place_init_t, Args&&... args )
437 construct(in_place_init, boost::forward<Args>(args)...);
440 template<class... Args>
441 explicit optional_base ( in_place_init_if_t, bool cond, Args&&... args )
446 construct(in_place_init, boost::forward<Args>(args)...);
448 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
450 void construct ( in_place_init_t, Arg&& arg )
452 ::new (m_storage.address()) value_type( boost::forward<Arg>(arg) );
453 m_initialized = true ;
456 void construct ( in_place_init_t )
458 ::new (m_storage.address()) value_type();
459 m_initialized = true ;
463 void emplace_assign ( Arg&& arg )
466 construct(in_place_init, boost::forward<Arg>(arg)) ;
469 void emplace_assign ()
472 construct(in_place_init) ;
476 explicit optional_base ( in_place_init_t, Arg&& arg )
480 construct(in_place_init, boost::forward<Arg>(arg));
483 explicit optional_base ( in_place_init_t )
487 construct(in_place_init);
491 explicit optional_base ( in_place_init_if_t, bool cond, Arg&& arg )
496 construct(in_place_init, boost::forward<Arg>(arg));
499 explicit optional_base ( in_place_init_if_t, bool cond )
504 construct(in_place_init);
510 void construct ( in_place_init_t, const Arg& arg )
512 ::new (m_storage.address()) value_type( arg );
513 m_initialized = true ;
517 void construct ( in_place_init_t, Arg& arg )
519 ::new (m_storage.address()) value_type( arg );
520 m_initialized = true ;
523 void construct ( in_place_init_t )
525 ::new (m_storage.address()) value_type();
526 m_initialized = true ;
530 void emplace_assign ( const Arg& arg )
533 construct(in_place_init, arg);
537 void emplace_assign ( Arg& arg )
540 construct(in_place_init, arg);
543 void emplace_assign ()
546 construct(in_place_init);
550 explicit optional_base ( in_place_init_t, const Arg& arg )
551 : m_initialized(false)
553 construct(in_place_init, arg);
557 explicit optional_base ( in_place_init_t, Arg& arg )
558 : m_initialized(false)
560 construct(in_place_init, arg);
563 explicit optional_base ( in_place_init_t )
564 : m_initialized(false)
566 construct(in_place_init);
570 explicit optional_base ( in_place_init_if_t, bool cond, const Arg& arg )
571 : m_initialized(false)
574 construct(in_place_init, arg);
578 explicit optional_base ( in_place_init_if_t, bool cond, Arg& arg )
579 : m_initialized(false)
582 construct(in_place_init, arg);
585 explicit optional_base ( in_place_init_if_t, bool cond )
586 : m_initialized(false)
589 construct(in_place_init);
593 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
595 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
596 // Constructs in-place using the given factory
598 void construct ( Expr&& factory, in_place_factory_base const* )
600 boost_optional_detail::construct<value_type>(factory, m_storage.address());
601 m_initialized = true ;
604 // Constructs in-place using the given typed factory
606 void construct ( Expr&& factory, typed_in_place_factory_base const* )
608 factory.apply(m_storage.address()) ;
609 m_initialized = true ;
613 void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
616 construct(factory,tag);
619 // Constructs in-place using the given typed factory
621 void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
624 construct(factory,tag);
628 // Constructs in-place using the given factory
630 void construct ( Expr const& factory, in_place_factory_base const* )
632 boost_optional_detail::construct<value_type>(factory, m_storage.address());
633 m_initialized = true ;
636 // Constructs in-place using the given typed factory
638 void construct ( Expr const& factory, typed_in_place_factory_base const* )
640 factory.apply(m_storage.address()) ;
641 m_initialized = true ;
645 void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
648 construct(factory,tag);
651 // Constructs in-place using the given typed factory
653 void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
656 construct(factory,tag);
662 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
663 // Constructs using any expression implicitly convertible to the single argument
664 // of a one-argument T constructor.
665 // Converting constructions of optional<T> from optional<U> uses this function with
666 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
668 void construct ( Expr&& expr, void const* )
670 new (m_storage.address()) value_type(boost::forward<Expr>(expr)) ;
671 m_initialized = true ;
674 // Assigns using a form any expression implicitly convertible to the single argument
675 // of a T's assignment operator.
676 // Converting assignments of optional<T> from optional<U> uses this function with
677 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
679 void assign_expr_to_initialized ( Expr&& expr, void const* )
681 assign_value( boost::forward<Expr>(expr) );
684 // Constructs using any expression implicitly convertible to the single argument
685 // of a one-argument T constructor.
686 // Converting constructions of optional<T> from optional<U> uses this function with
687 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
689 void construct ( Expr const& expr, void const* )
691 new (m_storage.address()) value_type(expr) ;
692 m_initialized = true ;
695 // Assigns using a form any expression implicitly convertible to the single argument
696 // of a T's assignment operator.
697 // Converting assignments of optional<T> from optional<U> uses this function with
698 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
700 void assign_expr_to_initialized ( Expr const& expr, void const* )
707 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
708 // BCB5.64 (and probably lower versions) workaround.
709 // The in-place factories are supported by means of catch-all constructors
710 // and assignment operators (the functions are parameterized in terms of
711 // an arbitrary 'Expr' type)
712 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
713 // to the 'Expr'-taking functions even though explicit overloads are present for them.
714 // Thus, the following overload is needed to properly handle the case when the 'lhs'
715 // is another optional.
717 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
718 // instead of choosing the wrong overload
720 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
721 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
723 void construct ( Expr&& expr, optional_tag const* )
725 if ( expr.is_initialized() )
727 // An exception can be thrown here.
728 // It it happens, THIS will be left uninitialized.
729 new (m_storage.address()) value_type(boost::move(expr.get())) ;
730 m_initialized = true ;
734 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
736 void construct ( Expr const& expr, optional_tag const* )
738 if ( expr.is_initialized() )
740 // An exception can be thrown here.
741 // It it happens, THIS will be left uninitialized.
742 new (m_storage.address()) value_type(expr.get()) ;
743 m_initialized = true ;
747 #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
749 void assign_value ( argument_type val ) { get_impl() = val; }
750 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
751 void assign_value ( rval_reference_type val ) { get_impl() = static_cast<rval_reference_type>(val); }
760 reference_const_type get_impl() const { return m_storage.ref() ; }
761 reference_type get_impl() { return m_storage.ref() ; }
763 pointer_const_type get_ptr_impl() const { return m_storage.ptr_ref(); }
764 pointer_type get_ptr_impl() { return m_storage.ptr_ref(); }
768 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1900))
769 void destroy_impl ( ) { m_storage.ptr_ref()->~T() ; m_initialized = false ; }
771 void destroy_impl ( ) { m_storage.ref().T::~T() ; m_initialized = false ; }
775 storage_type m_storage ;
778 #include <boost/optional/detail/optional_trivially_copyable_base.hpp>
780 // definition of metafunciton is_optional_val_init_candidate
781 template <typename U>
782 struct is_optional_related
783 : boost::conditional< boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
784 || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, none_t>::value
785 || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_t>::value
786 || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, in_place_init_if_t>::value,
787 boost::true_type, boost::false_type>::type
790 #if !defined(BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT)
792 template <typename T, typename U>
793 struct is_convertible_to_T_or_factory
794 : boost::conditional< boost::is_base_of<boost::in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
795 || boost::is_base_of<boost::typed_in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
796 || (boost::is_constructible<T, U&&>::value && !boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value)
797 , boost::true_type, boost::false_type>::type
800 template <typename T, typename U>
801 struct is_optional_constructible : boost::is_constructible<T, U>
806 template <typename, typename>
807 struct is_convertible_to_T_or_factory : boost::true_type
810 template <typename T, typename U>
811 struct is_optional_constructible : boost::true_type
814 #endif // is_convertible condition
816 template <typename T, typename U>
817 struct is_optional_val_init_candidate
818 : boost::conditional< !is_optional_related<U>::value && is_convertible_to_T_or_factory<T, U>::value
819 , boost::true_type, boost::false_type>::type
822 } // namespace optional_detail
824 namespace optional_config {
826 template <typename T>
827 struct optional_uses_direct_storage_for
828 : boost::conditional<(boost::is_scalar<T>::value && !boost::is_const<T>::value && !boost::is_volatile<T>::value)
829 , boost::true_type, boost::false_type>::type
832 } // namespace optional_config
835 #ifndef BOOST_OPTIONAL_DETAIL_NO_DIRECT_STORAGE_SPEC
836 # define BOOST_OPTIONAL_BASE_TYPE(T) boost::conditional< optional_config::optional_uses_direct_storage_for<T>::value, \
837 optional_detail::tc_optional_base<T>, \
838 optional_detail::optional_base<T> \
841 # define BOOST_OPTIONAL_BASE_TYPE(T) optional_detail::optional_base<T>
846 : public BOOST_OPTIONAL_BASE_TYPE(T)
848 typedef typename BOOST_OPTIONAL_BASE_TYPE(T) base ;
852 typedef optional<T> this_type ;
854 typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
855 typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
856 typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
857 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
858 typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
859 typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
861 typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
862 typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
863 typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
865 // Creates an optional<T> uninitialized.
867 optional() BOOST_NOEXCEPT : base() {}
869 // Creates an optional<T> uninitialized.
871 optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
873 // Creates an optional<T> initialized with 'val'.
874 // Can throw if T::T(T const&) does
875 optional ( argument_type val ) : base(optional_detail::init_value_tag(), val) {}
877 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
878 // Creates an optional<T> initialized with 'move(val)'.
879 // Can throw if T::T(T &&) does
880 optional ( rval_reference_type val ) : base(optional_detail::init_value_tag(), boost::forward<T>(val))
884 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
885 // Can throw if T::T(T const&) does
886 optional ( bool cond, argument_type val ) : base(cond,val) {}
888 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
889 /// Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
890 // Can throw if T::T(T &&) does
891 optional ( bool cond, rval_reference_type val ) : base( cond, boost::forward<T>(val) )
895 // NOTE: MSVC needs templated versions first
897 // Creates a deep copy of another convertible optional<U>
898 // Requires a valid conversion from U to T.
899 // Can throw if T::T(U const&) does
901 explicit optional ( optional<U> const& rhs
902 #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
903 ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U const&>, bool>::type = true
909 if ( rhs.is_initialized() )
910 this->construct(rhs.get());
913 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
914 // Creates a deep move of another convertible optional<U>
915 // Requires a valid conversion from U to T.
916 // Can throw if T::T(U&&) does
918 explicit optional ( optional<U> && rhs
919 #ifndef BOOST_OPTIONAL_DETAIL_NO_SFINAE_FRIENDLY_CONSTRUCTORS
920 ,BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_constructible<T, U>, bool>::type = true
926 if ( rhs.is_initialized() )
927 this->construct( boost::move(rhs.get()) );
931 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
932 // Creates an optional<T> with an expression which can be either
933 // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
934 // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
935 // (c) Any expression implicitly convertible to the single type
936 // of a one-argument T's constructor.
937 // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
938 // even though explicit overloads are present for these.
939 // Depending on the above some T ctor is called.
940 // Can throw if the resolved T ctor throws.
941 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
945 explicit optional ( Expr&& expr,
946 BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr>, bool>::type = true
948 : base(boost::forward<Expr>(expr),boost::addressof(expr))
953 explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
954 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
955 #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
957 // Creates a deep copy of another optional<T>
958 // Can throw if T::T(T const&) does
959 #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
960 optional ( optional const& ) = default;
962 optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
965 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
966 // Creates a deep move of another optional<T>
967 // Can throw if T::T(T&&) does
969 #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
970 optional ( optional && rhs ) = default;
972 optional ( optional && rhs )
973 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
974 : base( boost::move(rhs) )
980 #if BOOST_WORKAROUND(_MSC_VER, <= 1600)
981 // On old MSVC compilers the implicitly declared dtor is not called
986 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
987 // Assigns from an expression. See corresponding constructor.
988 // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
989 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
992 BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_init_candidate<T, Expr>, optional&>::type
993 operator= ( Expr&& expr )
995 this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
1000 template<class Expr>
1001 optional& operator= ( Expr const& expr )
1003 this->assign_expr(expr,boost::addressof(expr));
1006 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1007 #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
1009 // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
1010 // Requires a valid conversion from U to T.
1011 // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
1013 optional& operator= ( optional<U> const& rhs )
1019 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1020 // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
1021 // Requires a valid conversion from U to T.
1022 // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
1024 optional& operator= ( optional<U> && rhs )
1026 this->assign(boost::move(rhs));
1031 // Assigns from another optional<T> (deep-copies the rhs value)
1032 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
1033 // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
1034 #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
1035 optional& operator= ( optional const& rhs ) = default;
1037 optional& operator= ( optional const& rhs )
1039 this->assign( static_cast<base const&>(rhs) ) ;
1044 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1045 // Assigns from another optional<T> (deep-moves the rhs value)
1046 #ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
1047 optional& operator= ( optional && ) = default;
1049 optional& operator= ( optional && rhs )
1050 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
1052 this->assign( static_cast<base &&>(rhs) ) ;
1057 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1059 #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
1061 // Assigns from a T (deep-moves/copies the rhs value)
1062 template <typename T_>
1063 BOOST_DEDUCED_TYPENAME boost::enable_if<boost::is_same<T, BOOST_DEDUCED_TYPENAME boost::decay<T_>::type>, optional&>::type
1064 operator= ( T_&& val )
1066 this->assign( boost::forward<T_>(val) ) ;
1072 // Assigns from a T (deep-copies the rhs value)
1073 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
1074 optional& operator= ( argument_type val )
1076 this->assign( val ) ;
1080 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1081 // Assigns from a T (deep-moves the rhs value)
1082 optional& operator= ( rval_reference_type val )
1084 this->assign( boost::move(val) ) ;
1089 #endif // BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
1091 // Assigns from a "none"
1092 // Which destroys the current value, if any, leaving this UNINITIALIZED
1093 // No-throw (assuming T::~T() doesn't)
1094 optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
1096 this->assign( none_ ) ;
1100 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
1101 // Constructs in-place
1102 // upon exception *this is always uninitialized
1103 template<class... Args>
1104 void emplace ( Args&&... args )
1106 this->emplace_assign( boost::forward<Args>(args)... );
1109 template<class... Args>
1110 explicit optional ( in_place_init_t, Args&&... args )
1111 : base( in_place_init, boost::forward<Args>(args)... )
1114 template<class... Args>
1115 explicit optional ( in_place_init_if_t, bool cond, Args&&... args )
1116 : base( in_place_init_if, cond, boost::forward<Args>(args)... )
1119 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
1121 void emplace ( Arg&& arg )
1123 this->emplace_assign( boost::forward<Arg>(arg) );
1128 this->emplace_assign();
1131 template<class Args>
1132 explicit optional ( in_place_init_t, Args&& args )
1133 : base( in_place_init, boost::forward<Args>(args) )
1136 explicit optional ( in_place_init_t )
1137 : base( in_place_init )
1140 template<class Args>
1141 explicit optional ( in_place_init_if_t, bool cond, Args&& args )
1142 : base( in_place_init_if, cond, boost::forward<Args>(args) )
1145 explicit optional ( in_place_init_if_t, bool cond )
1146 : base( in_place_init_if, cond )
1150 void emplace ( const Arg& arg )
1152 this->emplace_assign( arg );
1156 void emplace ( Arg& arg )
1158 this->emplace_assign( arg );
1163 this->emplace_assign();
1167 explicit optional ( in_place_init_t, const Arg& arg )
1168 : base( in_place_init, arg )
1172 explicit optional ( in_place_init_t, Arg& arg )
1173 : base( in_place_init, arg )
1176 explicit optional ( in_place_init_t )
1177 : base( in_place_init )
1181 explicit optional ( in_place_init_if_t, bool cond, const Arg& arg )
1182 : base( in_place_init_if, cond, arg )
1186 explicit optional ( in_place_init_if_t, bool cond, Arg& arg )
1187 : base( in_place_init_if, cond, arg )
1190 explicit optional ( in_place_init_if_t, bool cond )
1191 : base( in_place_init_if, cond )
1195 void swap( optional & arg )
1196 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
1198 // allow for Koenig lookup
1199 boost::swap(*this, arg);
1203 // Returns a reference to the value if this is initialized, otherwise,
1204 // the behaviour is UNDEFINED
1206 reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
1207 reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
1209 // Returns a copy of the value if this is initialized, 'v' otherwise
1210 reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
1211 reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
1213 // Returns a pointer to the value if this is initialized, otherwise,
1214 // the behaviour is UNDEFINED
1216 pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
1217 pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
1219 // Returns a reference to the value if this is initialized, otherwise,
1220 // the behaviour is UNDEFINED
1222 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
1223 reference_const_type operator *() const& { return this->get() ; }
1224 reference_type operator *() & { return this->get() ; }
1225 reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
1227 reference_const_type operator *() const { return this->get() ; }
1228 reference_type operator *() { return this->get() ; }
1229 #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
1231 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
1232 reference_const_type value() const&
1234 if (this->is_initialized())
1235 return this->get() ;
1237 throw_exception(bad_optional_access());
1240 reference_type value() &
1242 if (this->is_initialized())
1243 return this->get() ;
1245 throw_exception(bad_optional_access());
1248 reference_type_of_temporary_wrapper value() &&
1250 if (this->is_initialized())
1251 return boost::move(this->get()) ;
1253 throw_exception(bad_optional_access());
1257 reference_const_type value() const
1259 if (this->is_initialized())
1260 return this->get() ;
1262 throw_exception(bad_optional_access());
1265 reference_type value()
1267 if (this->is_initialized())
1268 return this->get() ;
1270 throw_exception(bad_optional_access());
1275 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
1277 value_type value_or ( U&& v ) const&
1279 if (this->is_initialized())
1282 return boost::forward<U>(v);
1286 value_type value_or ( U&& v ) &&
1288 if (this->is_initialized())
1289 return boost::move(get());
1291 return boost::forward<U>(v);
1293 #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1295 value_type value_or ( U&& v ) const
1297 if (this->is_initialized())
1300 return boost::forward<U>(v);
1304 value_type value_or ( U const& v ) const
1306 if (this->is_initialized())
1313 value_type value_or ( U& v ) const
1315 if (this->is_initialized())
1323 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
1324 template <typename F>
1325 value_type value_or_eval ( F f ) const&
1327 if (this->is_initialized())
1333 template <typename F>
1334 value_type value_or_eval ( F f ) &&
1336 if (this->is_initialized())
1337 return boost::move(get());
1342 template <typename F>
1343 optional<typename boost::result_of<F(reference_type)>::type> map(F f) &
1345 if (this->has_value())
1351 template <typename F>
1352 optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const&
1354 if (this->has_value())
1360 template <typename F>
1361 optional<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type> map(F f) &&
1363 if (this->has_value())
1364 return f(boost::move(this->get()));
1369 template <typename F>
1370 optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f) &
1372 if (this->has_value())
1378 template <typename F>
1379 optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const&
1381 if (this->has_value())
1387 template <typename F>
1388 optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type_of_temporary_wrapper)>::type>::type> flat_map(F f) &&
1390 if (this->has_value())
1391 return f(boost::move(get()));
1397 template <typename F>
1398 value_type value_or_eval ( F f ) const
1400 if (this->is_initialized())
1406 template <typename F>
1407 optional<typename boost::result_of<F(reference_type)>::type> map(F f)
1409 if (this->has_value())
1415 template <typename F>
1416 optional<typename boost::result_of<F(reference_const_type)>::type> map(F f) const
1418 if (this->has_value())
1424 template <typename F>
1425 optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_type)>::type>::type> flat_map(F f)
1427 if (this->has_value())
1433 template <typename F>
1434 optional<typename optional_detail::optional_value_type<typename boost::result_of<F(reference_const_type)>::type>::type> flat_map(F f) const
1436 if (this->has_value())
1444 bool has_value() const BOOST_NOEXCEPT { return this->is_initialized() ; }
1446 bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
1448 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
1451 } // namespace boost
1453 #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
1457 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1461 BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal.");
1465 } // namespace boost
1467 #ifndef BOOST_OPTIONAL_CONFIG_DONT_SPECIALIZE_OPTIONAL_REFS
1468 # include <boost/optional/detail/optional_reference_spec.hpp>
1473 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1477 optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( T && v )
1479 return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(boost::forward<T>(v));
1482 // Returns optional<T>(cond,v)
1485 optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type> make_optional ( bool cond, T && v )
1487 return optional<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>(cond,boost::forward<T>(v));
1492 // Returns optional<T>(v)
1495 optional<T> make_optional ( T const& v )
1497 return optional<T>(v);
1500 // Returns optional<T>(cond,v)
1503 optional<T> make_optional ( bool cond, T const& v )
1505 return optional<T>(cond,v);
1508 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1510 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1514 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
1515 get ( optional<T> const& opt )
1522 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1523 get ( optional<T>& opt )
1528 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1532 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1533 get ( optional<T> const* opt )
1535 return opt->get_ptr() ;
1540 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1541 get ( optional<T>* opt )
1543 return opt->get_ptr() ;
1546 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1550 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
1551 get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
1553 return opt.get_value_or(v) ;
1558 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1559 get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
1561 return opt.get_value_or(v) ;
1564 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1568 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1569 get_pointer ( optional<T> const& opt )
1571 return opt.get_ptr() ;
1576 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1577 get_pointer ( optional<T>& opt )
1579 return opt.get_ptr() ;
1582 } // namespace boost
1586 // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
1587 template<class CharType, class CharTrait>
1588 std::basic_ostream<CharType, CharTrait>&
1589 operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&)
1591 BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
1595 } // namespace boost
1597 #include <boost/optional/detail/optional_relops.hpp>
1598 #include <boost/optional/detail/optional_swap.hpp>
1600 #endif // header guard