]> git.lyx.org Git - lyx.git/blob - 3rdparty/boost/boost/optional/optional.hpp
de.po
[lyx.git] / 3rdparty / boost / boost / optional / optional.hpp
1 // Copyright (C) 2003, 2008 Fernando Luis Cacciola Carballal.
2 // Copyright (C) 2014, 2015 Andrzej Krzemienski.
3 //
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)
7 //
8 // See http://www.boost.org/libs/optional for documentation.
9 //
10 // You are welcome to contact the author at:
11 //  fernando_cacciola@hotmail.com
12 //
13 // Revisions:
14 // 27 Apr 2008 (improved swap) Fernando Cacciola, Niels Dekker, Thorsten Ottosen
15 // 05 May 2014 (Added move semantics) Andrzej Krzemienski
16 //
17 #ifndef BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
18 #define BOOST_OPTIONAL_OPTIONAL_FLC_19NOV2002_HPP
19
20 #include <new>
21 #include <iosfwd>
22
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>
49
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>
54
55 #ifdef BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
56 #include <boost/optional/detail/old_optional_implementation.hpp>
57 #else
58 namespace boost {
59
60 namespace optional_detail {
61
62 struct optional_tag {} ;
63
64
65 template<class T>
66 class optional_base : public optional_tag
67 {
68   private :
69
70     typedef aligned_storage<T> storage_type ;
71     typedef optional_base<T> this_type ;
72
73   protected :
74
75     typedef T value_type ;
76
77   protected:
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 ;
83 #endif
84     typedef T *         pointer_type ;
85     typedef T const*    pointer_const_type ;
86     typedef T const&    argument_type ;
87
88     // Creates an optional<T> uninitialized.
89     // No-throw
90     optional_base()
91       :
92       m_initialized(false) {}
93
94     // Creates an optional<T> uninitialized.
95     // No-throw
96     optional_base ( none_t )
97       :
98       m_initialized(false) {}
99
100     // Creates an optional<T> initialized with 'val'.
101     // Can throw if T::T(T const&) does
102     optional_base ( argument_type val )
103       :
104       m_initialized(false)
105     {
106         construct(val);
107     }
108
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 )
113       :
114       m_initialized(false)
115     {
116       construct( boost::move(val) );
117     }
118 #endif
119
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 )
123       :
124       m_initialized(false)
125     {
126       if ( cond )
127         construct(val);
128     }
129
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 )
133       :
134       m_initialized(false)
135     {
136       if ( rhs.is_initialized() )
137         construct(rhs.get_impl());
138     }
139
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 )
144       :
145       m_initialized(false)
146     {
147       if ( rhs.is_initialized() )
148         construct( boost::move(rhs.get_impl()) );
149     }
150 #endif
151
152 #ifndef  BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
153
154     template<class Expr, class PtrExpr>
155     explicit optional_base ( Expr&& expr, PtrExpr const* tag )
156       :
157       m_initialized(false)
158     {
159       construct(boost::forward<Expr>(expr),tag);
160     }
161
162 #else
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)
166     template<class Expr>
167     explicit optional_base ( Expr const& expr, Expr const* tag )
168       :
169       m_initialized(false)
170     {
171       construct(expr,tag);
172     }
173
174 #endif
175
176
177     // No-throw (assuming T::~T() doesn't)
178     ~optional_base() { destroy() ; }
179
180     // Assigns from another optional<T> (deep-copies the rhs value)
181     void assign ( optional_base const& rhs )
182     {
183       if (is_initialized())
184       {
185         if ( rhs.is_initialized() )
186              assign_value(rhs.get_impl());
187         else destroy();
188       }
189       else
190       {
191         if ( rhs.is_initialized() )
192           construct(rhs.get_impl());
193       }
194     }
195     
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 )
199     {
200       if (is_initialized())
201       {
202         if ( rhs.is_initialized() )
203              assign_value( boost::move(rhs.get_impl()) );
204         else destroy();
205       }
206       else
207       {
208         if ( rhs.is_initialized() )
209           construct(boost::move(rhs.get_impl()));
210       }
211     }
212 #endif 
213
214     // Assigns from another _convertible_ optional<U> (deep-copies the rhs value)
215     template<class U>
216     void assign ( optional<U> const& rhs )
217     {
218       if (is_initialized())
219       {
220         if ( rhs.is_initialized() )
221 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
222           assign_value( rhs.get() );
223 #else
224           assign_value( static_cast<value_type>(rhs.get()) );
225 #endif
226           
227         else destroy();
228       }
229       else
230       {
231         if ( rhs.is_initialized() )
232 #ifndef BOOST_OPTIONAL_CONFIG_RESTORE_ASSIGNMENT_OF_NONCONVERTIBLE_TYPES
233           construct(rhs.get());
234 #else
235           construct(static_cast<value_type>(rhs.get()));
236 #endif
237       }
238     }
239
240 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
241     // move-assigns from another _convertible_ optional<U> (deep-moves from the rhs value)
242     template<class U>
243     void assign ( optional<U>&& rhs )
244     {
245       typedef BOOST_DEDUCED_TYPENAME optional<U>::rval_reference_type ref_type;
246       if (is_initialized())
247       {
248         if ( rhs.is_initialized() )
249              assign_value( static_cast<ref_type>(rhs.get()) );
250         else destroy();
251       }
252       else
253       {
254         if ( rhs.is_initialized() )
255           construct(static_cast<ref_type>(rhs.get()));
256       }
257     }
258 #endif
259     
260     // Assigns from a T (deep-copies the rhs value)
261     void assign ( argument_type val )
262     {
263       if (is_initialized())
264            assign_value(val);
265       else construct(val);
266     }
267     
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 )
271     {
272       if (is_initialized())
273            assign_value( boost::move(val) );
274       else construct( boost::move(val) );
275     }
276 #endif
277
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(); }
281
282 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
283
284 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
285     template<class Expr, class ExprPtr>
286     void assign_expr ( Expr&& expr, ExprPtr const* tag )
287     {
288       if (is_initialized())
289         assign_expr_to_initialized(boost::forward<Expr>(expr),tag);
290       else construct(boost::forward<Expr>(expr),tag);
291     }
292 #else
293     template<class Expr>
294     void assign_expr ( Expr const& expr, Expr const* tag )
295     {
296       if (is_initialized())
297         assign_expr_to_initialized(expr,tag);
298       else construct(expr,tag);
299     }
300 #endif
301
302 #endif
303
304   public :
305
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(); }
309
310     // **DEPPRECATED** Replaces the current value -if any- with 'val'
311     void reset ( argument_type val ) { assign(val); }
312
313     // Returns a pointer to the value if this is initialized, otherwise,
314     // returns NULL.
315     // No-throw
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 ; }
318
319     bool is_initialized() const { return m_initialized ; }
320
321   protected :
322
323     void construct ( argument_type val )
324      {
325        ::new (m_storage.address()) value_type(val) ;
326        m_initialized = true ;
327      }
328      
329 #ifndef  BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
330     void construct ( rval_reference_type val )
331      {
332        ::new (m_storage.address()) value_type( boost::move(val) ) ;
333        m_initialized = true ;
334      }
335 #endif
336
337
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 )
343      {
344        destroy();
345        ::new (m_storage.address()) value_type( boost::forward<Args>(args)... );
346        m_initialized = true ;
347      }
348 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
349     template<class Arg>
350     void emplace_assign ( Arg&& arg )
351      {
352        destroy();
353        ::new (m_storage.address()) value_type( boost::forward<Arg>(arg) );
354        m_initialized = true ;
355      }
356      
357     void emplace_assign ()
358      {
359        destroy();
360        ::new (m_storage.address()) value_type();
361        m_initialized = true ;
362      }
363 #else
364     template<class Arg>
365     void emplace_assign ( const Arg& arg )
366      {
367        destroy();
368        ::new (m_storage.address()) value_type( arg );
369        m_initialized = true ;
370      }
371      
372     template<class Arg>
373     void emplace_assign ( Arg& arg )
374      {
375        destroy();
376        ::new (m_storage.address()) value_type( arg );
377        m_initialized = true ;
378      }
379      
380     void emplace_assign ()
381      {
382        destroy();
383        ::new (m_storage.address()) value_type();
384        m_initialized = true ;
385      }
386 #endif
387
388 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
389
390 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
391     // Constructs in-place using the given factory
392     template<class Expr>
393     void construct ( Expr&& factory, in_place_factory_base const* )
394      {
395        boost_optional_detail::construct<value_type>(factory, m_storage.address());
396        m_initialized = true ;
397      }
398
399     // Constructs in-place using the given typed factory
400     template<class Expr>
401     void construct ( Expr&& factory, typed_in_place_factory_base const* )
402      {
403        factory.apply(m_storage.address()) ;
404        m_initialized = true ;
405      }
406
407     template<class Expr>
408     void assign_expr_to_initialized ( Expr&& factory, in_place_factory_base const* tag )
409      {
410        destroy();
411        construct(factory,tag);
412      }
413
414     // Constructs in-place using the given typed factory
415     template<class Expr>
416     void assign_expr_to_initialized ( Expr&& factory, typed_in_place_factory_base const* tag )
417      {
418        destroy();
419        construct(factory,tag);
420      }
421
422 #else
423     // Constructs in-place using the given factory
424     template<class Expr>
425     void construct ( Expr const& factory, in_place_factory_base const* )
426      {
427        boost_optional_detail::construct<value_type>(factory, m_storage.address());
428        m_initialized = true ;
429      }
430
431     // Constructs in-place using the given typed factory
432     template<class Expr>
433     void construct ( Expr const& factory, typed_in_place_factory_base const* )
434      {
435        factory.apply(m_storage.address()) ;
436        m_initialized = true ;
437      }
438
439     template<class Expr>
440     void assign_expr_to_initialized ( Expr const& factory, in_place_factory_base const* tag )
441      {
442        destroy();
443        construct(factory,tag);
444      }
445
446     // Constructs in-place using the given typed factory
447     template<class Expr>
448     void assign_expr_to_initialized ( Expr const& factory, typed_in_place_factory_base const* tag )
449      {
450        destroy();
451        construct(factory,tag);
452      }
453 #endif
454
455 #endif
456
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.
462     template<class Expr>
463     void construct ( Expr&& expr, void const* )
464     {
465       new (m_storage.address()) value_type(boost::forward<Expr>(expr)) ;
466       m_initialized = true ;
467     }
468
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.
473     template<class Expr>
474     void assign_expr_to_initialized ( Expr&& expr, void const* )
475     {
476       assign_value( boost::forward<Expr>(expr) );
477     }
478 #else
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.
483     template<class Expr>
484     void construct ( Expr const& expr, void const* )
485      {
486        new (m_storage.address()) value_type(expr) ;
487        m_initialized = true ;
488      }
489
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.
494     template<class Expr>
495     void assign_expr_to_initialized ( Expr const& expr, void const* )
496      {
497        assign_value(expr);
498      }
499
500 #endif
501
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.
511     //
512     // For VC<=70 compilers this workaround dosen't work becasue the comnpiler issues and error
513     // instead of choosing the wrong overload
514     //
515 #ifndef  BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
516     // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
517     template<class Expr>
518     void construct ( Expr&& expr, optional_tag const* )
519      {
520        if ( expr.is_initialized() )
521        {
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 ;
526        }
527      }
528 #else
529     // Notice that 'Expr' will be optional<T> or optional<U> (but not optional_base<..>)
530     template<class Expr>
531     void construct ( Expr const& expr, optional_tag const* )
532      {
533        if ( expr.is_initialized() )
534        {
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 ;
539        }
540      }
541 #endif
542 #endif // defined BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
543
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); }
547 #endif
548
549     void destroy()
550     {
551       if ( m_initialized )
552         destroy_impl() ;
553     }
554
555     reference_const_type get_impl() const { return m_storage.ref() ; }
556     reference_type       get_impl()       { return m_storage.ref() ; }
557
558     pointer_const_type get_ptr_impl() const { return m_storage.ptr_ref(); }
559     pointer_type       get_ptr_impl()       { return m_storage.ptr_ref(); }
560
561   private :
562
563 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1600)
564     void destroy_impl ( ) { m_storage.ptr_ref()->~T() ; m_initialized = false ; }
565 #else
566     void destroy_impl ( ) { m_storage.ref().T::~T() ; m_initialized = false ; }
567 #endif
568
569     bool m_initialized ;
570     storage_type m_storage ;
571 } ;
572
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
579 {};
580
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
584   
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
591 {};
592
593 #else
594
595 #define BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT
596
597 template <typename, typename>
598 struct is_convertible_to_T_or_factory : boost::true_type
599 {};
600
601 #endif // is_convertible condition
602
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
607 {};
608     
609 } // namespace optional_detail
610
611 template<class T>
612 class optional : public optional_detail::optional_base<T>
613 {
614     typedef optional_detail::optional_base<T> base ;
615
616   public :
617
618     typedef optional<T> this_type ;
619
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 ;
626 #endif
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 ;
630
631     // Creates an optional<T> uninitialized.
632     // No-throw
633     optional() BOOST_NOEXCEPT : base() {}
634
635     // Creates an optional<T> uninitialized.
636     // No-throw
637     optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
638
639     // Creates an optional<T> initialized with 'val'.
640     // Can throw if T::T(T const&) does
641     optional ( argument_type val ) : base(val) {}
642
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) ) 
647       {}
648 #endif
649
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) {}
653
654     // NOTE: MSVC needs templated versions first
655
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
659     template<class U>
660     explicit optional ( optional<U> const& rhs )
661       :
662       base()
663     {
664       if ( rhs.is_initialized() )
665         this->construct(rhs.get());
666     }
667     
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
672     template<class U>
673     explicit optional ( optional<U> && rhs )
674       :
675       base()
676     {
677       if ( rhs.is_initialized() )
678         this->construct( boost::move(rhs.get()) );
679     }
680 #endif
681
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
693
694
695   template<class Expr>
696   explicit optional ( Expr&& expr, 
697                       BOOST_DEDUCED_TYPENAME boost::enable_if< optional_detail::is_optional_val_init_candidate<T, Expr> >::type* = 0 
698   ) 
699     : base(boost::forward<Expr>(expr),boost::addressof(expr)) 
700     {}
701
702 #else
703     template<class 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
707
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) ) {}
711
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) ) 
718         {}
719
720 #endif
721
722 #if BOOST_WORKAROUND(_MSC_VER, <= 1600)
723     //  On old MSVC compilers the implicitly declared dtor is not called
724     ~optional() {}
725 #endif
726
727         
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
732
733     template<class Expr>
734     BOOST_DEDUCED_TYPENAME boost::enable_if<optional_detail::is_optional_val_init_candidate<T, Expr>, optional&>::type 
735     operator= ( Expr&& expr )
736       {
737         this->assign_expr(boost::forward<Expr>(expr),boost::addressof(expr));
738         return *this ;
739       }
740
741 #else
742     template<class Expr>
743     optional& operator= ( Expr const& expr )
744       {
745         this->assign_expr(expr,boost::addressof(expr));
746         return *this ;
747       }
748 #endif // !defined  BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
749 #endif // !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION)
750
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
754     template<class U>
755     optional& operator= ( optional<U> const& rhs )
756       {
757         this->assign(rhs);
758         return *this ;
759       }
760       
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
765     template<class U>
766     optional& operator= ( optional<U> && rhs )
767       {
768         this->assign(boost::move(rhs));
769         return *this ;
770       }
771 #endif
772
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 )
777       {
778         this->assign( static_cast<base const&>(rhs) ) ;
779         return *this ;
780       }
781
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)
786       {
787         this->assign( static_cast<base &&>(rhs) ) ;
788         return *this ;
789       }
790 #endif
791
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 )
795       {
796         this->assign( val ) ;
797         return *this ;
798       }
799
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 )
803       {
804         this->assign( boost::move(val) ) ;
805         return *this ;
806       }
807 #endif
808
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
813       {
814         this->assign( none_ ) ;
815         return *this ;
816       }
817       
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 )
823      {
824        this->emplace_assign( boost::forward<Args>(args)... );
825      }
826 #elif (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
827     template<class Arg>
828     void emplace ( Arg&& arg )
829      {
830        this->emplace_assign( boost::forward<Arg>(arg) );
831      }
832      
833     void emplace ()
834      {
835        this->emplace_assign();
836      }
837 #else
838     template<class Arg>
839     void emplace ( const Arg& arg )
840      {
841        this->emplace_assign( arg );
842      }
843      
844     template<class Arg>
845     void emplace ( Arg& arg )
846      {
847        this->emplace_assign( arg );
848      }
849      
850     void emplace ()
851      {
852        this->emplace_assign();
853      }
854 #endif
855
856     void swap( optional & arg )
857           BOOST_NOEXCEPT_IF(::boost::is_nothrow_move_constructible<T>::value && ::boost::is_nothrow_move_assignable<T>::value)
858       {
859         // allow for Koenig lookup
860         boost::swap(*this, arg);
861       }
862
863
864     // Returns a reference to the value if this is initialized, otherwise,
865     // the behaviour is UNDEFINED
866     // No-throw
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(); }
869
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 ; }
873
874     // Returns a pointer to the value if this is initialized, otherwise,
875     // the behaviour is UNDEFINED
876     // No-throw
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() ; }
879
880     // Returns a reference to the value if this is initialized, otherwise,
881     // the behaviour is UNDEFINED
882     // No-throw
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()) ; }
887 #else
888     reference_const_type operator *() const { return this->get() ; }
889     reference_type       operator *()       { return this->get() ; }
890 #endif // !defined BOOST_NO_CXX11_REF_QUALIFIERS
891
892 #if (!defined BOOST_NO_CXX11_REF_QUALIFIERS) && (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) 
893     reference_const_type value() const&
894       { 
895         if (this->is_initialized())
896           return this->get() ;
897         else
898           throw_exception(bad_optional_access());
899       }
900       
901     reference_type value() &
902       { 
903         if (this->is_initialized())
904           return this->get() ;
905         else
906           throw_exception(bad_optional_access());
907       }
908       
909     reference_type_of_temporary_wrapper value() &&
910       { 
911         if (this->is_initialized())
912           return boost::move(this->get()) ;
913         else
914           throw_exception(bad_optional_access());
915       }
916
917 #else 
918     reference_const_type value() const
919       { 
920         if (this->is_initialized())
921           return this->get() ;
922         else
923           throw_exception(bad_optional_access());
924       }
925       
926     reference_type value()
927       { 
928         if (this->is_initialized())
929           return this->get() ;
930         else
931           throw_exception(bad_optional_access());
932       }
933 #endif
934
935
936 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
937     template <class U>
938     value_type value_or ( U&& v ) const&
939       { 
940         if (this->is_initialized())
941           return get();
942         else
943           return boost::forward<U>(v);
944       }
945     
946     template <class U>
947     value_type value_or ( U&& v ) && 
948       { 
949         if (this->is_initialized())
950           return boost::move(get());
951         else
952           return boost::forward<U>(v);
953       }
954 #elif !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
955     template <class U>
956     value_type value_or ( U&& v ) const 
957       {
958         if (this->is_initialized())
959           return get();
960         else
961           return boost::forward<U>(v);
962       }
963 #else
964     template <class U>
965     value_type value_or ( U const& v ) const 
966       { 
967         if (this->is_initialized())
968           return get();
969         else
970           return v;
971       }
972       
973     template <class U>
974     value_type value_or ( U& v ) const 
975       { 
976         if (this->is_initialized())
977           return get();
978         else
979           return v;
980       }
981 #endif
982
983
984 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
985     template <typename F>
986     value_type value_or_eval ( F f ) const&
987       {
988         if (this->is_initialized())
989           return get();
990         else
991           return f();
992       }
993       
994     template <typename F>
995     value_type value_or_eval ( F f ) &&
996       {
997         if (this->is_initialized())
998           return boost::move(get());
999         else
1000           return f();
1001       }
1002 #else
1003     template <typename F>
1004     value_type value_or_eval ( F f ) const
1005       {
1006         if (this->is_initialized())
1007           return get();
1008         else
1009           return f();
1010       }
1011 #endif
1012       
1013     bool operator!() const BOOST_NOEXCEPT { return !this->is_initialized() ; }
1014     
1015     BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
1016 } ;
1017
1018 } // namespace boost
1019
1020 #endif // BOOST_OPTIONAL_CONFIG_USE_OLD_IMPLEMENTATION_OF_OPTIONAL
1021
1022 namespace boost {
1023   
1024 #ifndef  BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
1025 template<class T>
1026 class optional<T&&>
1027 {
1028   BOOST_STATIC_ASSERT_MSG(sizeof(T) == 0, "Optional rvalue references are illegal.");
1029 } ;
1030 #endif
1031
1032 } // namespace boost
1033
1034 #ifndef BOOST_OPTIONAL_CONFIG_DONT_SPECIALIZE_OPTIONAL_REFS
1035 # include <boost/optional/detail/optional_reference_spec.hpp>
1036 #endif
1037
1038 namespace boost {
1039
1040 // Returns optional<T>(v)
1041 template<class T>
1042 inline
1043 optional<T> make_optional ( T const& v  )
1044 {
1045   return optional<T>(v);
1046 }
1047
1048 // Returns optional<T>(cond,v)
1049 template<class T>
1050 inline
1051 optional<T> make_optional ( bool cond, T const& v )
1052 {
1053   return optional<T>(cond,v);
1054 }
1055
1056 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1057 // No-throw
1058 template<class T>
1059 inline
1060 BOOST_DEDUCED_TYPENAME optional<T>::reference_const_type
1061 get ( optional<T> const& opt )
1062 {
1063   return opt.get() ;
1064 }
1065
1066 template<class T>
1067 inline
1068 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1069 get ( optional<T>& opt )
1070 {
1071   return opt.get() ;
1072 }
1073
1074 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1075 // No-throw
1076 template<class T>
1077 inline
1078 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1079 get ( optional<T> const* opt )
1080 {
1081   return opt->get_ptr() ;
1082 }
1083
1084 template<class T>
1085 inline
1086 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1087 get ( optional<T>* opt )
1088 {
1089   return opt->get_ptr() ;
1090 }
1091
1092 // Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.
1093 // No-throw
1094 template<class T>
1095 inline
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 )
1098 {
1099   return opt.get_value_or(v) ;
1100 }
1101
1102 template<class T>
1103 inline
1104 BOOST_DEDUCED_TYPENAME optional<T>::reference_type
1105 get_optional_value_or ( optional<T>& opt, BOOST_DEDUCED_TYPENAME optional<T>::reference_type v )
1106 {
1107   return opt.get_value_or(v) ;
1108 }
1109
1110 // Returns a pointer to the value if this is initialized, otherwise, returns NULL.
1111 // No-throw
1112 template<class T>
1113 inline
1114 BOOST_DEDUCED_TYPENAME optional<T>::pointer_const_type
1115 get_pointer ( optional<T> const& opt )
1116 {
1117   return opt.get_ptr() ;
1118 }
1119
1120 template<class T>
1121 inline
1122 BOOST_DEDUCED_TYPENAME optional<T>::pointer_type
1123 get_pointer ( optional<T>& opt )
1124 {
1125   return opt.get_ptr() ;
1126 }
1127
1128 } // namespace boost
1129
1130 namespace boost {
1131   
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&)
1136 {
1137   BOOST_STATIC_ASSERT_MSG(sizeof(CharType) == 0, "If you want to output boost::optional, include header <boost/optional/optional_io.hpp>");
1138   return os;  
1139 }
1140
1141 } // namespace boost
1142
1143 #include <boost/optional/detail/optional_relops.hpp>
1144 #include <boost/optional/detail/optional_swap.hpp>
1145
1146 #endif // header guard