1 // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
2 // Copyright (C) 2014-2016 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 maintainer at:
13 #ifndef BOOST_OPTIONAL_DETAIL_OLD_OPTIONAL_IMPLEMENTATION_AJK_28JAN2015_HPP
14 #define BOOST_OPTIONAL_DETAIL_OLD_OPTIONAL_IMPLEMENTATION_AJK_28JAN2015_HPP
16 #include <boost/detail/reference_content.hpp>
17 #include <boost/type_traits/is_reference.hpp>
18 #include <boost/type_traits/integral_constant.hpp>
19 #include <boost/type_traits/conditional.hpp>
23 namespace optional_detail {
27 struct types_when_isnt_ref
29 typedef T const& reference_const_type ;
30 typedef T & reference_type ;
31 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
32 typedef T && rval_reference_type ;
33 typedef T && reference_type_of_temporary_wrapper;
34 #ifdef BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES
35 // GCC 4.4 has support for an early draft of rvalue references. The conforming version below
36 // causes warnings about returning references to a temporary.
37 static T&& move(T&& r) { return r; }
39 static rval_reference_type move(reference_type r) { return boost::move(r); }
42 typedef T const* pointer_const_type ;
43 typedef T * pointer_type ;
44 typedef T const& argument_type ;
48 struct types_when_is_ref
50 typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type raw_type ;
52 typedef raw_type& reference_const_type ;
53 typedef raw_type& reference_type ;
54 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
55 typedef BOOST_DEDUCED_TYPENAME remove_const<raw_type>::type&& rval_reference_type ;
56 typedef raw_type& reference_type_of_temporary_wrapper;
57 static reference_type move(reference_type r) { return r; }
59 typedef raw_type* pointer_const_type ;
60 typedef raw_type* pointer_type ;
61 typedef raw_type& argument_type ;
64 template <class To, class From>
65 void prevent_binding_rvalue_ref_to_optional_lvalue_ref()
67 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
68 BOOST_STATIC_ASSERT_MSG(
69 !boost::is_lvalue_reference<To>::value || !boost::is_rvalue_reference<From>::value,
70 "binding rvalue references to optional lvalue references is disallowed");
74 struct optional_tag {} ;
77 class optional_base : public optional_tag
82 #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
83 BOOST_DEDUCED_TYPENAME
85 ::boost::detail::make_reference_content<T>::type internal_type ;
87 typedef aligned_storage<internal_type> storage_type ;
89 typedef types_when_isnt_ref<T> types_when_not_ref ;
90 typedef types_when_is_ref<T> types_when_ref ;
92 typedef optional_base<T> this_type ;
96 typedef T value_type ;
98 typedef true_type is_reference_tag ;
99 typedef false_type is_not_reference_tag ;
101 typedef BOOST_DEDUCED_TYPENAME is_reference<T>::type is_reference_predicate ;
104 typedef BOOST_DEDUCED_TYPENAME conditional<is_reference_predicate::value,types_when_ref,types_when_not_ref>::type types ;
107 typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
108 typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
109 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
110 typedef BOOST_DEDUCED_TYPENAME types::rval_reference_type rval_reference_type ;
111 typedef BOOST_DEDUCED_TYPENAME types::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
113 typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
114 typedef BOOST_DEDUCED_TYPENAME types::pointer_const_type pointer_const_type ;
115 typedef BOOST_DEDUCED_TYPENAME types::argument_type argument_type ;
117 // Creates an optional<T> uninitialized.
121 m_initialized(false) {}
123 // Creates an optional<T> uninitialized.
125 optional_base ( none_t )
127 m_initialized(false) {}
129 // Creates an optional<T> initialized with 'val'.
130 // Can throw if T::T(T const&) does
131 optional_base ( argument_type val )
138 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
139 // move-construct an optional<T> initialized from an rvalue-ref to 'val'.
140 // Can throw if T::T(T&&) does
141 optional_base ( rval_reference_type val )
145 construct( boost::move(val) );
149 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional<T>.
150 // Can throw if T::T(T const&) does
151 optional_base ( bool cond, argument_type val )
159 // Creates a deep copy of another optional<T>
160 // Can throw if T::T(T const&) does
161 optional_base ( optional_base const& rhs )
165 if ( rhs.is_initialized() )
166 construct(rhs.get_impl());
169 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
170 // Creates a deep move of another optional<T>
171 // Can throw if T::T(T&&) does
172 optional_base ( optional_base&& rhs )
176 if ( rhs.is_initialized() )
177 construct( boost::move(rhs.get_impl()) );
181 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
183 template<class Expr, class PtrExpr>
184 explicit optional_base ( Expr&& expr, PtrExpr const* tag )
188 construct(boost::forward<Expr>(expr),tag);
192 // This is used for both converting and in-place constructions.
193 // Derived classes use the 'tag' to select the appropriate
194 // implementation (the correct 'construct()' overload)
196 explicit optional_base ( Expr const& expr, Expr const* tag )
206 // No-throw (assuming T::~T() doesn't)
207 ~optional_base() { destroy() ; }
209 // Assigns from another optional<T> (deep-copies the rhs value)
210 void assign ( optional_base const& rhs )
212 if (is_initialized())
214 if ( rhs.is_initialized() )
215 assign_value(rhs.get_impl(), is_reference_predicate() );
220 if ( rhs.is_initialized() )
221 construct(rhs.get_impl());
225 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
226 // Assigns from another optional<T> (deep-moves the rhs value)
227 void assign ( optional_base&& rhs )
229 if (is_initialized())
231 if ( rhs.is_initialized() )
232 assign_value(boost::move(rhs.get_impl()), is_reference_predicate() );
237 if ( rhs.is_initialized() )
238 construct(boost::move(rhs.get_impl()));
243 // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
245 void assign ( optional<U> const& rhs )
247 if (is_initialized())
249 if ( rhs.is_initialized() )
250 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
251 assign_value(rhs.get(), is_reference_predicate() );
253 assign_value(static_cast<value_type>(rhs.get()), is_reference_predicate() );
260 if ( rhs.is_initialized() )
261 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
262 construct(rhs.get());
264 construct(static_cast<value_type>(rhs.get()));
269 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
270 // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
272 void assign ( optional<U>&& rhs )
274 typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
275 if (is_initialized())
277 if ( rhs.is_initialized() )
278 assign_value(static_cast<ref_type>(rhs.get()), is_reference_predicate() );
283 if ( rhs.is_initialized() )
284 construct(static_cast<ref_type>(rhs.get()));
289 // Assigns from a T (deep-copies the rhs value)
290 void assign ( argument_type val )
292 if (is_initialized())
293 assign_value(val, is_reference_predicate() );
297 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
298 // Assigns from a T (deep-moves the rhs value)
299 void assign ( rval_reference_type val )
301 if (is_initialized())
302 assign_value( boost::move(val), is_reference_predicate() );
303 else construct( boost::move(val) );
307 // Assigns from "none", destroying the current value, if any, leaving this UNINITIALIZED
308 // No-throw (assuming T::~T() doesn't)
309 void assign ( none_t ) BOOST_NOEXCEPT { destroy(); }
311 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
313 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
314 template<class Expr, class ExprPtr>
315 void assign_expr ( Expr&& expr, ExprPtr const* tag )
317 if (is_initialized())
318 assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
319 else construct(boost::forward<Expr>(expr),tag);
323 void assign_expr ( Expr const& expr, Expr const* tag )
325 if (is_initialized())
326 assign_expr_to_initialized(expr,tag);
327 else construct(expr,tag);
335 // Destroys the current value, if any, leaving this UNINITIALIZED
336 // No-throw (assuming T::~T() doesn't)
337 void reset() BOOST_NOEXCEPT { destroy(); }
339 // **DEPPRECATED** Replaces the current value -if any- with 'val'
340 void reset ( argument_type val ) { assign(val); }
342 // Returns a pointer to the value if this is initialized, otherwise,
345 pointer_const_type get_ptr() const { return m_initialized ? get_ptr_impl() : 0 ; }
346 pointer_type get_ptr() { return m_initialized ? get_ptr_impl() : 0 ; }
348 bool is_initialized() const { return m_initialized ; }
352 void construct ( argument_type val )
354 ::new (m_storage.address()) internal_type(val) ;
355 m_initialized = true ;
358 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
359 void construct ( rval_reference_type val )
361 ::new (m_storage.address()) internal_type( types::move(val) ) ;
362 m_initialized = true ;
367 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
368 // Constructs in-place
369 // upon exception *this is always uninitialized
370 template<class... Args>
371 void emplace_assign ( Args&&... args )
374 ::new (m_storage.address()) internal_type( boost::forward<Args>(args)... );
375 m_initialized = true ;
377 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
379 void emplace_assign ( Arg&& arg )
382 ::new (m_storage.address()) internal_type( boost::forward<Arg>(arg) );
383 m_initialized = true ;
386 void emplace_assign ()
389 ::new (m_storage.address()) internal_type();
390 m_initialized = true ;
394 void emplace_assign ( const Arg& arg )
397 ::new (m_storage.address()) internal_type( arg );
398 m_initialized = true ;
402 void emplace_assign ( Arg& arg )
405 ::new (m_storage.address()) internal_type( arg );
406 m_initialized = true ;
409 void emplace_assign ()
412 ::new (m_storage.address()) internal_type();
413 m_initialized = true ;
417 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
419 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
420 // Constructs in-place using the given factory
422 void construct ( Expr&& factory, in_place_factory_base const* )
424 BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
425 boost_optional_detail::construct<value_type>(factory, m_storage.address());
426 m_initialized = true ;
429 // Constructs in-place using the given typed factory
431 void construct ( Expr&& factory, typed_in_place_factory_base const* )
433 BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
434 factory.apply(m_storage.address()) ;
435 m_initialized = true ;
439 void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
442 construct(factory,tag);
445 // Constructs in-place using the given typed factory
447 void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
450 construct(factory,tag);
454 // Constructs in-place using the given factory
456 void construct ( Expr const& factory, in_place_factory_base const* )
458 BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
459 boost_optional_detail::construct<value_type>(factory, m_storage.address());
460 m_initialized = true ;
463 // Constructs in-place using the given typed factory
465 void construct ( Expr const& factory, typed_in_place_factory_base const* )
467 BOOST_STATIC_ASSERT ( !is_reference_predicate::value ) ;
468 factory.apply(m_storage.address()) ;
469 m_initialized = true ;
473 void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
476 construct(factory,tag);
479 // Constructs in-place using the given typed factory
481 void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
484 construct(factory,tag);
490 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
491 // Constructs using any expression implicitly convertible to the single argument
492 // of a one-argument T constructor.
493 // Converting constructions of optional<T> from optional<U> uses this function with
494 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
496 void construct ( Expr&& expr, void const* )
498 new (m_storage.address()) internal_type(boost::forward<Expr>(expr)) ;
499 m_initialized = true ;
502 // Assigns using a form any expression implicitly convertible to the single argument
503 // of a T's assignment operator.
504 // Converting assignments of optional<T> from optional<U> uses this function with
505 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
507 void assign_expr_to_initialized ( Expr&& expr, void const* )
509 assign_value(boost::forward<Expr>(expr), is_reference_predicate());
512 // Constructs using any expression implicitly convertible to the single argument
513 // of a one-argument T constructor.
514 // Converting constructions of optional<T> from optional<U> uses this function with
515 // 'Expr' being of type 'U' and relying on a converting constructor of T from U.
517 void construct ( Expr const& expr, void const* )
519 new (m_storage.address()) internal_type(expr) ;
520 m_initialized = true ;
523 // Assigns using a form any expression implicitly convertible to the single argument
524 // of a T's assignment operator.
525 // Converting assignments of optional<T> from optional<U> uses this function with
526 // 'Expr' being of type 'U' and relying on a converting assignment of T from U.
528 void assign_expr_to_initialized ( Expr const& expr, void const* )
530 assign_value(expr, is_reference_predicate());
535 #ifdef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
536 // BCB5.64 (and probably lower versions) workaround.
537 // The in-place factories are supported by means of catch-all constructors
538 // and assignment operators (the functions are parameterized in terms of
539 // an arbitrary 'Expr' type)
540 // This compiler incorrectly resolves the overload set and sinks optional<T> and optional<U>
541 // to the 'Expr'-taking functions even though explicit overloads are present for them.
542 // Thus, the following overload is needed to properly handle the case when the 'lhs'
543 // is another optional.
545 // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
546 // instead of choosing the wrong overload
548 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
549 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
551 void construct ( Expr&& expr, optional_tag const* )
553 if ( expr.is_initialized() )
555 // An exception can be thrown here.
556 // It it happens, THIS will be left uninitialized.
557 new (m_storage.address()) internal_type(types::move(expr.get())) ;
558 m_initialized = true ;
562 // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
564 void construct ( Expr const& expr, optional_tag const* )
566 if ( expr.is_initialized() )
568 // An exception can be thrown here.
569 // It it happens, THIS will be left uninitialized.
570 new (m_storage.address()) internal_type(expr.get()) ;
571 m_initialized = true ;
575 #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
577 void assign_value ( argument_type val, is_not_reference_tag ) { get_impl() = val; }
578 void assign_value ( argument_type val, is_reference_tag ) { construct(val); }
579 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
580 void assign_value ( rval_reference_type val, is_not_reference_tag ) { get_impl() = static_cast<rval_reference_type>(val); }
581 void assign_value ( rval_reference_type val, is_reference_tag ) { construct( static_cast<rval_reference_type>(val) ); }
587 destroy_impl(is_reference_predicate()) ;
590 reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
591 reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
593 pointer_const_type get_ptr_impl() const { return cast_ptr(get_object(), is_reference_predicate() ) ; }
594 pointer_type get_ptr_impl() { return cast_ptr(get_object(), is_reference_predicate() ) ; }
598 // internal_type can be either T or reference_content<T>
599 #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS)
600 // This workaround is supposed to silence GCC warnings about broken strict aliasing rules
601 internal_type const* get_object() const
603 union { void const* ap_pvoid; internal_type const* as_ptype; } caster = { m_storage.address() };
604 return caster.as_ptype;
606 internal_type * get_object()
608 union { void* ap_pvoid; internal_type* as_ptype; } caster = { m_storage.address() };
609 return caster.as_ptype;
612 internal_type const* get_object() const { return static_cast<internal_type const*>(m_storage.address()); }
613 internal_type * get_object() { return static_cast<internal_type *> (m_storage.address()); }
616 // reference_content<T> lacks an implicit conversion to T&, so the following is needed to obtain a proper reference.
617 reference_const_type dereference( internal_type const* p, is_not_reference_tag ) const { return *p ; }
618 reference_type dereference( internal_type* p, is_not_reference_tag ) { return *p ; }
619 reference_const_type dereference( internal_type const* p, is_reference_tag ) const { return p->get() ; }
620 reference_type dereference( internal_type* p, is_reference_tag ) { return p->get() ; }
622 #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x581))
623 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->internal_type::~internal_type() ; m_initialized = false ; }
625 void destroy_impl ( is_not_reference_tag ) { get_ptr_impl()->~T() ; m_initialized = false ; }
628 void destroy_impl ( is_reference_tag ) { m_initialized = false ; }
630 // If T is of reference type, trying to get a pointer to the held value must result in a compile-time error.
631 // Decent compilers should disallow conversions from reference_content<T>* to T*, but just in case,
632 // the following olverloads are used to filter out the case and guarantee an error in case of T being a reference.
633 pointer_const_type cast_ptr( internal_type const* p, is_not_reference_tag ) const { return p ; }
634 pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; }
635 pointer_const_type cast_ptr( internal_type const* p, is_reference_tag ) const { return &p->get() ; }
636 pointer_type cast_ptr( internal_type * p, is_reference_tag ) { return &p->get() ; }
639 storage_type m_storage ;
642 } // namespace optional_detail
645 class optional : public optional_detail::optional_base<T>
647 typedef optional_detail::optional_base<T> base ;
651 typedef optional<T> this_type ;
653 typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ;
654 typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ;
655 typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ;
656 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
657 typedef BOOST_DEDUCED_TYPENAME base::rval_reference_type rval_reference_type ;
658 typedef BOOST_DEDUCED_TYPENAME base::reference_type_of_temporary_wrapper reference_type_of_temporary_wrapper ;
660 typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ;
661 typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ;
662 typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ;
664 // Creates an optional<T> uninitialized.
666 optional() BOOST_NOEXCEPT : base() {}
668 // Creates an optional<T> uninitialized.
670 optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
672 // Creates an optional<T> initialized with 'val'.
673 // Can throw if T::T(T const&) does
674 optional ( argument_type val ) : base(val) {}
676 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
677 // Creates an optional<T> initialized with 'move(val)'.
678 // Can throw if T::T(T &&) does
679 optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
680 {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, rval_reference_type>();}
683 // Creates an optional<T> initialized with 'val' IFF cond is true, otherwise creates an uninitialized optional.
684 // Can throw if T::T(T const&) does
685 optional ( bool cond, argument_type val ) : base(cond,val) {}
687 // NOTE: MSVC needs templated versions first
689 // Creates a deep copy of another convertible optional<U>
690 // Requires a valid conversion from U to T.
691 // Can throw if T::T(U const&) does
693 explicit optional ( optional<U> const& rhs )
697 if ( rhs.is_initialized() )
698 this->construct(rhs.get());
701 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
702 // Creates a deep move of another convertible optional<U>
703 // Requires a valid conversion from U to T.
704 // Can throw if T::T(U&&) does
706 explicit optional ( optional<U> && rhs )
710 if ( rhs.is_initialized() )
711 this->construct( boost::move(rhs.get()) );
715 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
716 // Creates an optional<T> with an expression which can be either
717 // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n);
718 // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n);
719 // (c) Any expression implicitly convertible to the single type
720 // of a one-argument T's constructor.
721 // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U>
722 // even though explicit overloads are present for these.
723 // Depending on the above some T ctor is called.
724 // Can throw if the resolved T ctor throws.
725 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
729 explicit optional ( Expr&& expr,
730 BOOST_DEDUCED_TYPENAME boost::disable_if_c<
731 (boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type>::value) ||
732 boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type, none_t>::value, bool >::type = true
734 : base(boost::forward<Expr>(expr),boost::addressof(expr))
735 {optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, Expr&&>();}
739 explicit optional ( Expr const& expr ) : base(expr,boost::addressof(expr)) {}
740 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
741 #endif // !defined BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
743 // Creates a deep copy of another optional<T>
744 // Can throw if T::T(T const&) does
745 optional ( optional const& rhs ) : base( static_cast<base const&>(rhs) ) {}
747 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
748 // Creates a deep move of another optional<T>
749 // Can throw if T::T(T&&) does
750 optional ( optional && rhs )
751 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value)
752 : base( boost::move(rhs) )
756 // No-throw (assuming T::~T() doesn't)
759 #if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
760 // Assigns from an expression. See corresponding constructor.
761 // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED
762 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
765 BOOST_DEDUCED_TYPENAME boost::disable_if_c<
766 boost::is_base_of<optional_detail::optional_tag, BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type>::value ||
767 boost::is_same<BOOST_DEDUCED_TYPENAME boost::decay<Expr>::type, none_t>::value,
770 operator= ( Expr&& expr )
772 optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, Expr&&>();
773 this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
779 optional& operator= ( Expr const& expr )
781 this->assign_expr(expr,boost::addressof(expr));
784 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
785 #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
787 // Copy-assigns from another convertible optional<U> (converts && deep-copies the rhs value)
788 // Requires a valid conversion from U to T.
789 // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED
791 optional& operator= ( optional<U> const& rhs )
797 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
798 // Move-assigns from another convertible optional<U> (converts && deep-moves the rhs value)
799 // Requires a valid conversion from U to T.
800 // Basic Guarantee: If T::T( U && ) throws, this is left UNINITIALIZED
802 optional& operator= ( optional<U> && rhs )
804 this->assign(boost::move(rhs));
809 // Assigns from another optional<T> (deep-copies the rhs value)
810 // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED
811 // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw)
812 optional& operator= ( optional const& rhs )
814 this->assign( static_cast<base const&>(rhs) ) ;
818 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
819 // Assigns from another optional<T> (deep-moves the rhs value)
820 optional& operator= ( optional && rhs )
821 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
823 this->assign( static_cast<base &&>(rhs) ) ;
828 // Assigns from a T (deep-copies the rhs value)
829 // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED
830 optional& operator= ( argument_type val )
832 this->assign( val ) ;
836 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
837 // Assigns from a T (deep-moves the rhs value)
838 optional& operator= ( rval_reference_type val )
840 optional_detail::prevent_binding_rvalue_ref_to_optional_lvalue_ref<T, rval_reference_type>();
841 this->assign( boost::move(val) ) ;
846 // Assigns from a "none"
847 // Which destroys the current value, if any, leaving this UNINITIALIZED
848 // No-throw (assuming T::~T() doesn't)
849 optional& operator= ( none_t none_ ) BOOST_NOEXCEPT
851 this->assign( none_ ) ;
855 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
856 // Constructs in-place
857 // upon exception *this is always uninitialized
858 template<class... Args>
859 void emplace ( Args&&... args )
861 this->emplace_assign( boost::forward<Args>(args)... );
863 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
865 void emplace ( Arg&& arg )
867 this->emplace_assign( boost::forward<Arg>(arg) );
872 this->emplace_assign();
876 void emplace ( const Arg& arg )
878 this->emplace_assign( arg );
882 void emplace ( Arg& arg )
884 this->emplace_assign( arg );
889 this->emplace_assign();
893 void swap( optional & arg )
894 BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
896 // allow for Koenig lookup
897 boost::swap(*this, arg);
901 // Returns a reference to the value if this is initialized, otherwise,
902 // the behaviour is UNDEFINED
904 reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
905 reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
907 // Returns a copy of the value if this is initialized, 'v' otherwise
908 reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
909 reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }
911 // Returns a pointer to the value if this is initialized, otherwise,
912 // the behaviour is UNDEFINED
914 pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
915 pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
917 // Returns a reference to the value if this is initialized, otherwise,
918 // the behaviour is UNDEFINED
920 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
921 reference_const_type operator *() const& { return this->get() ; }
922 reference_type operator *() & { return this->get() ; }
923 reference_type_of_temporary_wrapper operator *() && { return base::types::move(this->get()) ; }
925 reference_const_type operator *() const { return this->get() ; }
926 reference_type operator *() { return this->get() ; }
927 #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
929 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
930 reference_const_type value() const&
932 if (this->is_initialized())
935 throw_exception(bad_optional_access());
938 reference_type value() &
940 if (this->is_initialized())
943 throw_exception(bad_optional_access());
946 reference_type_of_temporary_wrapper value() &&
948 if (this->is_initialized())
949 return base::types::move(this->get()) ;
951 throw_exception(bad_optional_access());
955 reference_const_type value() const
957 if (this->is_initialized())
960 throw_exception(bad_optional_access());
963 reference_type value()
965 if (this->is_initialized())
968 throw_exception(bad_optional_access());
973 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
975 value_type value_or ( U&& v ) const&
977 if (this->is_initialized())
980 return boost::forward<U>(v);
984 value_type value_or ( U&& v ) &&
986 if (this->is_initialized())
987 return base::types::move(get());
989 return boost::forward<U>(v);
991 #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
993 value_type value_or ( U&& v ) const
995 if (this->is_initialized())
998 return boost::forward<U>(v);
1002 value_type value_or ( U const& v ) const
1004 if (this->is_initialized())
1011 value_type value_or ( U& v ) const
1013 if (this->is_initialized())
1021 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
1022 template <typename F>
1023 value_type value_or_eval ( F f ) const&
1025 if (this->is_initialized())
1031 template <typename F>
1032 value_type value_or_eval ( F f ) &&
1034 if (this->is_initialized())
1035 return base::types::move(get());
1040 template <typename F>
1041 value_type value_or_eval ( F f ) const
1043 if (this->is_initialized())
1050 bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
1052 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
1055 } // namespace boost
1058 #endif // header guard