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
23 #include <boost/assert.hpp>
24 #include <boost/core/addressof.hpp>
25 #include <boost/core/enable_if.hpp>
26 #include <boost/core/explicit_operator_bool.hpp>
27 #include <boost/core/swap.hpp>
28 #include <boost/optional/bad_optional_access.hpp>
29 #include <boost/static_assert.hpp>
30 #include <boost/throw_exception.hpp>
31 #include <boost/type.hpp>
32 #include <boost/type_traits/alignment_of.hpp>
33 #include <boost/type_traits/conditional.hpp>
34 #include <boost/type_traits/has_nothrow_constructor.hpp>
35 #include <boost/type_traits/type_with_alignment.hpp>
36 #include <boost/type_traits/remove_const.hpp>
37 #include <boost/type_traits/remove_reference.hpp>
38 #include <boost/type_traits/decay.hpp>
39 #include <boost/type_traits/is_base_of.hpp>
40 #include <boost/type_traits/is_constructible.hpp>
41 #include <boost/type_traits/is_lvalue_reference.hpp>
42 #include <boost/type_traits/is_nothrow_move_assignable.hpp>
43 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
44 #include <boost/type_traits/is_rvalue_reference.hpp>
45 #include <boost/type_traits/is_same.hpp>
46 #include <boost/move/utility.hpp>
47 #include <boost/none.hpp>
48 #include <boost/utility/compare_pointees.hpp>
50 #include <boost/optional/optional_fwd.hpp>
51 #include <boost/optional/detail/optional_config.hpp>
52 #include <boost/optional/detail/optional_factory_support.hpp>
53 #include <boost/optional/detail/optional_aligned_storage.hpp>
55 #ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
56 #include <boost/optional/detail/old_optional_implementation.hpp>
60 namespace optional_detail {
62 struct optional_tag {} ;
66 class optional_base : public optional_tag
70 typedef aligned_storage<T> storage_type ;
71 typedef optional_base<T> this_type ;
75 typedef T value_type ;
78 typedef T & reference_type ;
79 typedef T const& reference_const_type ;
80 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
81 typedef T && rval_reference_type ;
82 typedef T && reference_type_of_temporary_wrapper ;
84 typedef T * pointer_type ;
85 typedef T const* pointer_const_type ;
86 typedef T const& argument_type ;
88 // Creates an optional<T> uninitialized.
92 m_initialized(false) {}
94 // Creates an optional<T> uninitialized.
96 optional_base ( none_t )
98 m_initialized(false) {}
100 // Creates an optional<T> initialized with 'val'.
101 // Can throw if T::T(T const&) does
102 optional_base ( argument_type val )
109 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
110 // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
111 // Can throw if T::T(T&&) does
112 optional_base ( rval_reference_type val )
116 construct( boost::move(val) );
120 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialzed optional<T>.
121 // Can throw if T::T(T const&) does
122 optional_base ( bool cond, argument_type val )
130 // Creates a deep copy of another optional<T>
131 // Can throw if T::T(T const&) does
132 optional_base ( optional_base const& rhs )
136 if ( rhs.is_initialized() )
137 construct(rhs.get_impl());
140 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
141 // Creates a deep move of another optional<T>
142 // Can throw if T::T(T&&) does
143 optional_base ( optional_base&& rhs )
147 if ( rhs.is_initialized() )
148 construct( boost::move(rhs.get_impl()) );
152 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
154 template<class Expr, class PtrExpr>
155 explicit optional_base ( Expr&& expr, PtrExpr const* tag )
159 construct(boost::forward<Expr>(expr),tag);
163 // This is used for both converting and in-place constructions.
164 // Derived classes use the 'tag' to select the appropriate
165 // implementation (the correct 'construct()' overload)
167 explicit optional_base ( Expr const& expr, Expr const* tag )
177 // No-throw (assuming T::~T() doesn't)
178 ~optional_base() { destroy() ; }
180 // Assigns from another optional<T> (deep-copies the rhs value)
181 void assign ( optional_base const& rhs )
183 if (is_initialized())
185 if ( rhs.is_initialized() )
186 assign_value(rhs.get_impl());
191 if ( rhs.is_initialized() )
192 construct(rhs.get_impl());
196 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
197 // Assigns from another optional<T> (deep-moves the rhs value)
198 void assign ( optional_base&& rhs )
200 if (is_initialized())
202 if ( rhs.is_initialized() )
203 assign_value( boost::move(rhs.get_impl()) );
208 if ( rhs.is_initialized() )
209 construct(boost::move(rhs.get_impl()));
214 // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
216 void assign ( optional<U> const& rhs )
218 if (is_initialized())
220 if ( rhs.is_initialized() )
221 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
222 assign_value( rhs.get() );
224 assign_value( static_cast<value_type>(rhs.get()) );
231 if ( rhs.is_initialized() )
232 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
233 construct(rhs.get());
235 construct(static_cast<value_type>(rhs.get()));
240 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
241 // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
243 void assign ( optional<U>&& rhs )
245 typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
246 if (is_initialized())
248 if ( rhs.is_initialized() )
249 assign_value( static_cast<ref_type>(rhs.get()) );
254 if ( rhs.is_initialized() )
255 construct(static_cast<ref_type>(rhs.get()));
260 // Assigns from a T (deep-copies the rhs value)
261 void assign ( argument_type val )
263 if (is_initialized())
268 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
269 // Assigns from a T (deep-moves the rhs value)
270 void assign ( rval_reference_type val )
272 if (is_initialized())
273 assign_value( boost::move(val) );
274 else construct( boost::move(val) );
278 // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
279 // No-throw (assuming T::~T() doesn't)
280 void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
282 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
284 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
285 template<class Expr, class ExprPtr>
286 void assign_expr ( Expr&& expr, ExprPtr const* tag )
288 if (is_initialized())
289 assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
290 else construct(boost::forward<Expr>(expr),tag);
294 void assign_expr ( Expr const& expr, Expr const* tag )
296 if (is_initialized())
297 assign_expr_to_initialized(expr,tag);
298 else construct(expr,tag);
306 // **DEPPRECATED** Destroys the current value, if any, leaving this UNINITIALIZED
307 // No-throw (assuming T::~T() doesn't)
308 void reset() BOOST_NOEXCEPT { destroy(); }
310 // **DEPPRECATED** Replaces the current value -if any- with 'val'
311 void reset ( argument_type val ) { assign(val); }
313 // Returns a pointer to the value if this is initialized, otherwise,
316 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
317 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
319 bool is_initialized() const { return m_initialized ; }
323 void construct ( argument_type val )
325 ::new (m_storage.address()) value_type(val) ;
326 m_initialized = true ;
329 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
330 void construct ( rval_reference_type val )
332 ::new (m_storage.address()) value_type( boost::move(val) ) ;
333 m_initialized = true ;
338 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
339 // Constructs in-place
340 // upon exception *this is always uninitialized
341 template<class... Args>
342 void emplace_assign ( Args&&... args )
345 ::new (m_storage.address()) value_type( boost::forward<Args>(args)... );
346 m_initialized = true ;
348 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
350 void emplace_assign ( Arg&& arg )
353 ::new (m_storage.address()) value_type( boost::forward<Arg>(arg) );
354 m_initialized = true ;
357 void emplace_assign ()
360 ::new (m_storage.address()) value_type();
361 m_initialized = true ;
365 void emplace_assign ( const Arg& arg )
368 ::new (m_storage.address()) value_type( arg );
369 m_initialized = true ;
373 void emplace_assign ( Arg& arg )
376 ::new (m_storage.address()) value_type( arg );
377 m_initialized = true ;
380 void emplace_assign ()
383 ::new (m_storage.address()) value_type();
384 m_initialized = true ;
388 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
390 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
391 // Constructs in-place using the given factory
393 void construct ( Expr&& factory, in_place_factory_base const* )
395 boost_optional_detail::construct<value_type>(factory, m_storage.address());
396 m_initialized = true ;
399 // Constructs in-place using the given typed factory
401 void construct ( Expr&& factory, typed_in_place_factory_base const* )
403 factory.apply(m_storage.address()) ;
404 m_initialized = true ;
408 void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
411 construct(factory,tag);
414 // Constructs in-place using the given typed factory
416 void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
419 construct(factory,tag);
423 // Constructs in-place using the given factory
425 void construct ( Expr const& factory, in_place_factory_base const* )
427 boost_optional_detail::construct<value_type>(factory, m_storage.address());
428 m_initialized = true ;
431 // Constructs in-place using the given typed factory
433 void construct ( Expr const& factory, typed_in_place_factory_base const* )
435 factory.apply(m_storage.address()) ;
436 m_initialized = true ;
440 void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
443 construct(factory,tag);
446 // Constructs in-place using the given typed factory
448 void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
451 construct(factory,tag);
457 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
458 // Constructs using any expression implicitly convertible to the single argument
459 // of a one-argument T constructor.
460 // Converting constructions of optional<T> from optional<U> uses this function with
461 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
463 void construct ( Expr&& expr, void const* )
465 new (m_storage.address()) value_type(boost::forward<Expr>(expr)) ;
466 m_initialized = true ;
469 // Assigns using a form any expression implicitly convertible to the single argument
470 // of a T's assignment operator.
471 // Converting assignments of optional<T> from optional<U> uses this function with
472 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
474 void assign_expr_to_initialized ( Expr&& expr, void const* )
476 assign_value( boost::forward<Expr>(expr) );
479 // Constructs using any expression implicitly convertible to the single argument
480 // of a one-argument T constructor.
481 // Converting constructions of optional<T> from optional<U> uses this function with
482 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
484 void construct ( Expr const& expr, void const* )
486 new (m_storage.address()) value_type(expr) ;
487 m_initialized = true ;
490 // Assigns using a form any expression implicitly convertible to the single argument
491 // of a T's assignment operator.
492 // Converting assignments of optional<T> from optional<U> uses this function with
493 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
495 void assign_expr_to_initialized ( Expr const& expr, void const* )
502 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
503 // BCB5.64 (and probably lower versions) workaround.
504 // The in-place factories are supported by means of catch-all constructors
505 // and assignment operators (the functions are parameterized in terms of
506 // an arbitrary 'Expr' type)
507 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
508 // to the 'Expr'-taking functions even though explicit overloads are present for them.
509 // Thus, the following overload is needed to properly handle the case when the 'lhs'
510 // is another optional.
512 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
513 // instead of choosing the wrong overload
515 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
516 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
518 void construct ( Expr&& expr, optional_tag const* )
520 if ( expr.is_initialized() )
522 // An exception can be thrown here.
523 // It it happens, THIS will be left uninitialized.
524 new (m_storage.address()) value_type(boost::move(expr.get())) ;
525 m_initialized = true ;
529 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
531 void construct ( Expr const& expr, optional_tag const* )
533 if ( expr.is_initialized() )
535 // An exception can be thrown here.
536 // It it happens, THIS will be left uninitialized.
537 new (m_storage.address()) value_type(expr.get()) ;
538 m_initialized = true ;
542 #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
544 void assign_value ( argument_type val ) { get_impl() = val; }
545 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
546 void assign_value ( rval_reference_type val ) { get_impl() = static_cast<rval_reference_type>(val); }
555 reference_const_type get_impl() const { return m_storage.ref() ; }
556 reference_type get_impl() { return m_storage.ref() ; }
558 pointer_const_type get_ptr_impl() const { return m_storage.ptr_ref(); }
559 pointer_type get_ptr_impl() { return m_storage.ptr_ref(); }
563 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1600)
564 void destroy_impl ( ) { m_storage.ptr_ref()->~T() ; m_initialized = false ; }
566 void destroy_impl ( ) { m_storage.ref().T::~T() ; m_initialized = false ; }
570 storage_type m_storage ;
573 // definition of metafunciton is_optional_val_init_candidate
574 template <typename U>
575 struct is_optional_related
576 : boost::conditional< boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
577 || boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<U>::type, none_t>::value,
578 boost::true_type, boost::false_type>::type
581 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500) && !defined(__SUNPRO_CC)
582 // this condition is a copy paste from is_constructible.hpp
583 // I also disable SUNPRO, as it seems not to support type_traits correctly
585 template <typename T, typename U>
586 struct is_convertible_to_T_or_factory
587 : boost::conditional< boost::is_base_of<boost::in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
588 || boost::is_base_of<boost::typed_in_place_factory_base, BOOST_DEDUCED_TYPENAME boost::decay<U>::type>::value
589 || boost::is_constructible<T, U&&>::value
590 , boost::true_type, boost::false_type>::type
595 #define BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT
597 template <typename, typename>
598 struct is_convertible_to_T_or_factory : boost::true_type
601 #endif // is_convertible condition
603 template <typename T, typename U>
604 struct is_optional_val_init_candidate
605 : boost::conditional< !is_optional_related<U>::value && is_convertible_to_T_or_factory<T, U>::value
606 , boost::true_type, boost::false_type>::type
609 } // namespace optional_detail
612 class optional : public optional_detail::optional_base<T>
614 typedef optional_detail::optional_base<T> base ;
618 typedef optional<T> this_type ;
620 typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
621 typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
622 typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
623 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
624 typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
625 typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
627 typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
628 typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
629 typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
631 // Creates an optional<T> uninitialized.
633 optional() BOOST_NOEXCEPT : base() {}
635 // Creates an optional<T> uninitialized.
637 optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
639 // Creates an optional<T> initialized with 'val'.
640 // Can throw if T::T(T const&) does
641 optional ( argument_type val ) : base(val) {}
643 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
644 // Creates an optional<T> initialized with 'move(val)'.
645 // Can throw if T::T(T &&) does
646 optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
650 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
651 // Can throw if T::T(T const&) does
652 optional ( bool cond, argument_type val ) : base(cond,val) {}
654 // NOTE: MSVC needs templated versions first
656 // Creates a deep copy of another convertible optional<U>
657 // Requires a valid conversion from U to T.
658 // Can throw if T::T(U const&) does
660 explicit optional ( optional<U> const& rhs )
664 if ( rhs.is_initialized() )
665 this->construct(rhs.get());
668 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
669 // Creates a deep move of another convertible optional<U>
670 // Requires a valid conversion from U to T.
671 // Can throw if T::T(U&&) does
673 explicit optional ( optional<U> && rhs )
677 if ( rhs.is_initialized() )
678 this->construct( boost::move(rhs.get()) );
682 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
683 // Creates an optional<T> with an expression which can be either
684 // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
685 // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
686 // (c) Any expression implicitly convertible to the single type
687 // of a one-argument T's constructor.
688 // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
689 // even though explicit overloads are present for these.
690 // Depending on the above some T ctor is called.
691 // Can throw if the resolved T ctor throws.
692 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
696 explicit optional ( Expr&& expr,
697 BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr> >::type* = 0
699 : base(boost::forward<Expr>(expr),boost::addressof(expr))
704 explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
705 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
706 #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
708 // Creates a deep copy of another optional<T>
709 // Can throw if T::T(T const&) does
710 optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
712 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
713 // Creates a deep move of another optional<T>
714 // Can throw if T::T(T&&) does
715 optional ( optional && rhs )
716 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
717 : base( boost::move(rhs) )
722 #if BOOST_WORKAROUND(_MSC_VER, <= 1600)
723 // On old MSVC compilers the implicitly declared dtor is not called
728 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
729 // Assigns from an expression. See corresponding constructor.
730 // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
731 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
734 BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_init_candidate<T, Expr>, optional&>::type
735 operator= ( Expr&& expr )
737 this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
743 optional& operator= ( Expr const& expr )
745 this->assign_expr(expr,boost::addressof(expr));
748 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
749 #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
751 // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
752 // Requires a valid conversion from U to T.
753 // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
755 optional& operator= ( optional<U> const& rhs )
761 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
762 // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
763 // Requires a valid conversion from U to T.
764 // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
766 optional& operator= ( optional<U> && rhs )
768 this->assign(boost::move(rhs));
773 // Assigns from another optional<T> (deep-copies the rhs value)
774 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
775 // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
776 optional& operator= ( optional const& rhs )
778 this->assign( static_cast<base const&>(rhs) ) ;
782 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
783 // Assigns from another optional<T> (deep-moves the rhs value)
784 optional& operator= ( optional && rhs )
785 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
787 this->assign( static_cast<base &&>(rhs) ) ;
792 // Assigns from a T (deep-copies the rhs value)
793 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
794 optional& operator= ( argument_type val )
796 this->assign( val ) ;
800 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
801 // Assigns from a T (deep-moves the rhs value)
802 optional& operator= ( rval_reference_type val )
804 this->assign( boost::move(val) ) ;
809 // Assigns from a "none"
810 // Which destroys the current value, if any, leaving this UNINITIALIZED
811 // No-throw (assuming T::~T() doesn't)
812 optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
814 this->assign( none_ ) ;
818 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
819 // Constructs in-place
820 // upon exception *this is always uninitialized
821 template<class... Args>
822 void emplace ( Args&&... args )
824 this->emplace_assign( boost::forward<Args>(args)... );
826 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
828 void emplace ( Arg&& arg )
830 this->emplace_assign( boost::forward<Arg>(arg) );
835 this->emplace_assign();
839 void emplace ( const Arg& arg )
841 this->emplace_assign( arg );
845 void emplace ( Arg& arg )
847 this->emplace_assign( arg );
852 this->emplace_assign();
856 void swap( optional & arg )
857 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
859 // allow for Koenig lookup
860 boost::swap(*this, arg);
864 // Returns a reference to the value if this is initialized, otherwise,
865 // the behaviour is UNDEFINED
867 reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
868 reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
870 // Returns a copy of the value if this is initialized, 'v' otherwise
871 reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
872 reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
874 // Returns a pointer to the value if this is initialized, otherwise,
875 // the behaviour is UNDEFINED
877 pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
878 pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
880 // Returns a reference to the value if this is initialized, otherwise,
881 // the behaviour is UNDEFINED
883 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
884 reference_const_type operator *() const& { return this->get() ; }
885 reference_type operator *() & { return this->get() ; }
886 reference_type_of_temporary_wrapper operator *() && { return boost::move(this->get()) ; }
888 reference_const_type operator *() const { return this->get() ; }
889 reference_type operator *() { return this->get() ; }
890 #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
892 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
893 reference_const_type value() const&
895 if (this->is_initialized())
898 throw_exception(bad_optional_access());
901 reference_type value() &
903 if (this->is_initialized())
906 throw_exception(bad_optional_access());
909 reference_type_of_temporary_wrapper value() &&
911 if (this->is_initialized())
912 return boost::move(this->get()) ;
914 throw_exception(bad_optional_access());
918 reference_const_type value() const
920 if (this->is_initialized())
923 throw_exception(bad_optional_access());
926 reference_type value()
928 if (this->is_initialized())
931 throw_exception(bad_optional_access());
936 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
938 value_type value_or ( U&& v ) const&
940 if (this->is_initialized())
943 return boost::forward<U>(v);
947 value_type value_or ( U&& v ) &&
949 if (this->is_initialized())
950 return boost::move(get());
952 return boost::forward<U>(v);
954 #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
956 value_type value_or ( U&& v ) const
958 if (this->is_initialized())
961 return boost::forward<U>(v);
965 value_type value_or ( U const& v ) const
967 if (this->is_initialized())
974 value_type value_or ( U& v ) const
976 if (this->is_initialized())
984 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
985 template <typename F>
986 value_type value_or_eval ( F f ) const&
988 if (this->is_initialized())
994 template <typename F>
995 value_type value_or_eval ( F f ) &&
997 if (this->is_initialized())
998 return boost::move(get());
1003 template <typename F>
1004 value_type value_or_eval ( F f ) const
1006 if (this->is_initialized())
1013 bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
1015 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
1018 } // namespace boost
1020 #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
1024 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1028 BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal.");
1032 } // namespace boost
1034 #ifndef BOOST_OPTIONAL_CONFIG_DONT_SPECIALIZE_OPTIONAL_REFS
1035 # include <boost/optional/detail/optional_reference_spec.hpp>
1040 // Returns optional<T>(v)
1043 optional<T> make_optional ( T const& v )
1045 return optional<T>(v);
1048 // Returns optional<T>(cond,v)
1051 optional<T> make_optional ( bool cond, T const& v )
1053 return optional<T>(cond,v);
1056 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1060 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
1061 get ( optional<T> const& opt )
1068 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1069 get ( optional<T>& opt )
1074 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1078 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1079 get ( optional<T> const* opt )
1081 return opt->get_ptr() ;
1086 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1087 get ( optional<T>* opt )
1089 return opt->get_ptr() ;
1092 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1096 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
1097 get_optional_value_or ( optional<T> const& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type v )
1099 return opt.get_value_or(v) ;
1104 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1105 get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
1107 return opt.get_value_or(v) ;
1110 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1114 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1115 get_pointer ( optional<T> const& opt )
1117 return opt.get_ptr() ;
1122 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1123 get_pointer ( optional<T>& opt )
1125 return opt.get_ptr() ;
1128 } // namespace boost
1132 // The following declaration prevents a bug where operator safe-bool is used upon streaming optional object if you forget the IO header.
1133 template<class CharType, class CharTrait>
1134 std::basic_ostream<CharType, CharTrait>&
1135 operator<<(std::basic_ostream<CharType, CharTrait>& os, optional_detail::optional_tag const&)
1137 BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
1141 } // namespace boost
1143 #include <boost/optional/detail/optional_relops.hpp>
1144 #include <boost/optional/detail/optional_swap.hpp>
1146 #endif // header guard