1 //-----------------------------------------------------------------------------
2 // boost variant/variant.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
6 // Copyright (c) 2002-2003 Eric Friedman, Itay Maman
7 // Copyright (c) 2012-2019 Antony Polukhin
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 // Thanks to Adam Romanek for providing patches for exception-disabled env.
15 #ifndef BOOST_VARIANT_VARIANT_HPP
16 #define BOOST_VARIANT_VARIANT_HPP
18 #include <cstddef> // for std::size_t
19 #include <new> // for placement new
21 #include <boost/type_index.hpp>
23 #include <boost/variant/detail/config.hpp>
24 #include <boost/mpl/aux_/value_wknd.hpp>
26 #include <boost/variant/variant_fwd.hpp>
27 #include <boost/variant/detail/backup_holder.hpp>
28 #include <boost/variant/detail/enable_recursive_fwd.hpp>
29 #include <boost/variant/detail/forced_return.hpp>
30 #include <boost/variant/detail/initializer.hpp>
31 #include <boost/variant/detail/make_variant_list.hpp>
32 #include <boost/variant/detail/over_sequence.hpp>
33 #include <boost/variant/detail/visitation_impl.hpp>
34 #include <boost/variant/detail/hash_variant.hpp>
35 #include <boost/variant/detail/std_hash.hpp>
37 #include <boost/variant/detail/move.hpp>
39 #include <boost/detail/no_exceptions_support.hpp>
40 #include <boost/detail/reference_content.hpp>
41 #include <boost/aligned_storage.hpp>
42 #include <boost/blank.hpp>
43 #include <boost/integer/common_factor_ct.hpp>
44 #include <boost/static_assert.hpp>
45 #include <boost/preprocessor/cat.hpp>
46 #include <boost/preprocessor/repeat.hpp>
47 #include <boost/type_traits/alignment_of.hpp>
48 #include <boost/type_traits/add_const.hpp>
49 #include <boost/type_traits/has_nothrow_constructor.hpp>
50 #include <boost/type_traits/has_nothrow_copy.hpp>
51 #include <boost/type_traits/is_nothrow_move_assignable.hpp>
52 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
53 #include <boost/type_traits/is_const.hpp>
54 #include <boost/type_traits/is_same.hpp>
55 #include <boost/type_traits/is_rvalue_reference.hpp>
56 #include <boost/type_traits/is_constructible.hpp>
57 #include <boost/type_traits/add_lvalue_reference.hpp>
58 #include <boost/utility/enable_if.hpp>
59 #include <boost/utility/declval.hpp>
60 #include <boost/variant/recursive_wrapper_fwd.hpp>
61 #include <boost/variant/static_visitor.hpp>
63 #include <boost/mpl/assert.hpp>
64 #include <boost/mpl/begin_end.hpp>
65 #include <boost/mpl/bool.hpp>
66 #include <boost/mpl/deref.hpp>
67 #include <boost/mpl/empty.hpp>
68 #include <boost/mpl/eval_if.hpp>
69 #include <boost/mpl/find_if.hpp>
70 #include <boost/mpl/fold.hpp>
71 #include <boost/mpl/front.hpp>
72 #include <boost/mpl/identity.hpp>
73 #include <boost/mpl/if.hpp>
74 #include <boost/mpl/insert_range.hpp>
75 #include <boost/mpl/int.hpp>
76 #include <boost/mpl/is_sequence.hpp>
77 #include <boost/mpl/iterator_range.hpp>
78 #include <boost/mpl/iter_fold_if.hpp>
79 #include <boost/mpl/list.hpp>
80 #include <boost/mpl/logical.hpp>
81 #include <boost/mpl/max_element.hpp>
82 #include <boost/mpl/next.hpp>
83 #include <boost/mpl/not.hpp>
84 #include <boost/mpl/pair.hpp>
85 #include <boost/mpl/protect.hpp>
86 #include <boost/mpl/push_front.hpp>
87 #include <boost/mpl/same_as.hpp>
88 #include <boost/mpl/size_t.hpp>
89 #include <boost/mpl/sizeof.hpp>
90 #include <boost/mpl/transform.hpp>
92 ///////////////////////////////////////////////////////////////////////////////
93 // Implementation Macros:
95 // BOOST_VARIANT_VISITATION_UNROLLING_LIMIT
96 // Defined in boost/variant/detail/visitation_impl.hpp.
98 // BOOST_VARIANT_MINIMIZE_SIZE
99 // When #defined, implementation employs all known means to minimize the
100 // size of variant obje cts. However, often unsuccessful due to alignment
101 // issues, and potentially harmful to runtime speed, so not enabled by
102 // default. (TODO: Investigate further.)
104 #if defined(BOOST_VARIANT_MINIMIZE_SIZE)
105 # include <climits> // for SCHAR_MAX
106 # include <boost/mpl/eval_if.hpp>
107 # include <boost/mpl/equal_to.hpp>
108 # include <boost/mpl/identity.hpp>
109 # include <boost/mpl/int.hpp>
110 # include <boost/mpl/if.hpp>
111 # include <boost/mpl/less.hpp>
112 # include <boost/mpl/long.hpp>
113 # include <boost/mpl/O1_size.hpp>
119 namespace detail { namespace variant {
121 ///////////////////////////////////////////////////////////////////////////////
122 // (detail) metafunction max_value
124 // Finds the maximum value of the unary metafunction F over Sequence.
126 template <typename Sequence, typename F>
129 private: // helpers, for metafunction result (below)
131 typedef typename mpl::transform1<Sequence, F>::type transformed_;
132 typedef typename mpl::max_element<transformed_
136 public: // metafunction result
138 typedef typename mpl::deref<max_it>::type
145 template <typename State, typename Item>
148 ::boost::integer::static_lcm<
149 BOOST_MPL_AUX_VALUE_WKND(State)::value
150 , ::boost::alignment_of<Item>::value
156 ///////////////////////////////////////////////////////////////////////////////
157 // (detail) metafunction find_fallback_type
159 // Provides a fallback (i.e., nothrow default-constructible) type from the
160 // specified sequence, or no_fallback_type if not found.
162 // This implementation is designed to prefer boost::blank over other potential
163 // fallback types, regardless of its position in the specified sequence.
166 class no_fallback_type;
168 struct find_fallback_type_pred
170 template <typename Iterator>
174 typedef typename mpl::deref<Iterator>::type t_;
177 typedef mpl::not_< has_nothrow_constructor<t_> > type;
181 template <typename Types>
182 struct find_fallback_type
184 private: // helpers, for metafunction result (below)
186 typedef typename mpl::end<Types>::type end_it;
188 // [Find the first suitable fallback type...]
190 typedef typename mpl::iter_fold_if<
192 , mpl::int_<0>, mpl::protect< mpl::next<> >
193 , mpl::protect< find_fallback_type_pred >
194 >::type first_result_;
196 typedef typename first_result_::first first_result_index;
197 typedef typename first_result_::second first_result_it;
199 // [...now search the rest of the sequence for boost::blank...]
201 typedef typename mpl::iter_fold_if<
202 mpl::iterator_range< first_result_it,end_it >
203 , first_result_index, mpl::protect< mpl::next<> >
204 , mpl::protect< mpl::not_same_as<boost::blank> >
205 >::type second_result_;
207 typedef typename second_result_::second second_result_it;
209 public: // metafunction result
211 // [...and return the results of the search:]
212 typedef typename mpl::eval_if<
213 is_same< second_result_it,end_it >
215 is_same< first_result_it,end_it >
216 , mpl::pair< no_fallback_type,no_fallback_type >
219 , mpl::identity< second_result_ >
224 #ifndef BOOST_NO_CXX11_NOEXCEPT
225 ///////////////////////////////////////////////////////////////////////////////
226 // (detail) metafunction is_variant_move_noexcept_constructible
228 // Returns true_type if all the types are nothrow move constructible.
230 template <class Types>
231 struct is_variant_move_noexcept_constructible {
232 typedef typename boost::mpl::find_if<
233 Types, mpl::not_<boost::is_nothrow_move_constructible<boost::mpl::_1> >
236 typedef typename boost::mpl::end<Types>::type end_t;
237 typedef typename boost::is_same<
242 ///////////////////////////////////////////////////////////////////////////////
243 // (detail) metafunction is_variant_move_noexcept_assignable
245 // Returns true_type if all the types are nothrow move constructible.
247 template <class Types>
248 struct is_variant_move_noexcept_assignable {
249 typedef typename boost::mpl::find_if<
250 Types, mpl::not_<boost::is_nothrow_move_assignable<boost::mpl::_1> >
253 typedef typename boost::mpl::end<Types>::type end_t;
254 typedef typename boost::is_same<
258 #endif // BOOST_NO_CXX11_NOEXCEPT
260 ///////////////////////////////////////////////////////////////////////////////
261 // (detail) metafunction is_variant_constructible_from
263 // Derives from true_type if at least one variant's type is constructible from T.
265 template <class T1, class T2>
266 struct is_constructible_ext:
268 boost::is_constructible<
272 boost::is_constructible<
274 typename boost::add_lvalue_reference<T2>::type
279 template <class T, class Types>
280 struct is_variant_constructible_from:
281 boost::mpl::not_< boost::is_same<
282 typename boost::mpl::find_if<
284 is_constructible_ext<boost::mpl::_1, T>
286 typename boost::mpl::end<Types>::type
290 template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
291 struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >:
293 typename boost::mpl::find_if<
294 typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::recursive_enabled_types,
295 mpl::not_< is_variant_constructible_from< boost::mpl::_1, Types> >
297 typename boost::mpl::end< typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::recursive_enabled_types >::type
301 template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
302 struct is_variant_constructible_from< const boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& , Types >:
303 is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
306 template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
307 struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& , Types >:
308 is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
311 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
313 template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
314 struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>&& , Types >:
315 is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
318 template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
319 struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const && , Types >:
320 is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
323 #endif // #ifndef BOOST_NO_CXX11_RVALUE_REFERENCE
326 ///////////////////////////////////////////////////////////////////////////////
327 // (detail) metafunction make_storage
329 // Provides an aligned storage type capable of holding any of the types
330 // specified in the given type-sequence.
333 template <typename Types, typename NeverUsesBackupFlag>
336 private: // helpers, for metafunction result (below)
338 typedef typename mpl::eval_if<
340 , mpl::identity< Types >
342 Types, backup_holder<void*>
346 typedef typename max_value<
347 types, mpl::sizeof_<mpl::_1>
350 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
352 typedef typename mpl::fold<
356 >::type max_alignment;
360 // temporary workaround -- use maximal alignment
361 typedef mpl::size_t< -1 > max_alignment;
363 #endif // borland workaround
365 public: // metafunction result
367 typedef ::boost::aligned_storage<
368 BOOST_MPL_AUX_VALUE_WKND(max_size)::value
369 , BOOST_MPL_AUX_VALUE_WKND(max_alignment)::value
373 ///////////////////////////////////////////////////////////////////////////////
374 // (detail) class destroyer
376 // Internal visitor that destroys the value it visits.
379 : public static_visitor<>
381 public: // visitor interfaces
383 template <typename T>
384 void internal_visit(T& operand, int) const BOOST_NOEXCEPT
386 operand.~T(); // must be noexcept
388 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551)) || \
389 BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
390 (void)operand; // suppresses warnings
396 ///////////////////////////////////////////////////////////////////////////////
397 // (detail) class template known_get
399 // Visitor that returns a reference to content of the specified type.
401 // Precondition: visited variant MUST contain logical content of type T.
403 template <typename T>
405 : public static_visitor<T&>
408 public: // visitor interface
410 T& operator()(T& operand) const BOOST_NOEXCEPT
415 template <typename U>
416 T& operator()(U&) const
418 // logical error to be here: see precondition above
419 return ::boost::detail::variant::forced_return< T& >();
423 ///////////////////////////////////////////////////////////////////////////////
424 // (detail) class copy_into
426 // Internal visitor that copies the value it visits into the given buffer.
429 : public static_visitor<>
431 private: // representation
437 explicit copy_into(void* storage) BOOST_NOEXCEPT
442 public: // internal visitor interface
444 template <typename T>
445 void internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
447 new(storage_) T( operand.get() );
450 template <typename T>
451 void internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
453 new(storage_) T( operand.get() );
456 template <typename T>
457 void internal_visit(const T& operand, int) const
459 new(storage_) T(operand);
464 ///////////////////////////////////////////////////////////////////////////////
465 // (detail) class move_into
467 // Internal visitor that moves the value it visits into the given buffer.
469 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
471 : public static_visitor<>
473 private: // representation
479 explicit move_into(void* storage) BOOST_NOEXCEPT
484 public: // internal visitor interface
486 template <typename T>
487 void internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
489 new(storage_) T( ::boost::detail::variant::move(operand.get()) );
492 template <typename T>
493 void internal_visit(T& operand, int) const BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T(boost::declval<T>())))
495 new(storage_) T(::boost::detail::variant::move(operand));
500 ///////////////////////////////////////////////////////////////////////////////
501 // (detail) class assign_storage
503 // Internal visitor that assigns the given storage (which must be a
504 // constructed value of the same type) to the value it visits.
506 struct assign_storage
507 : public static_visitor<>
509 private: // representation
511 const void* rhs_storage_;
515 explicit assign_storage(const void* rhs_storage) BOOST_NOEXCEPT
516 : rhs_storage_(rhs_storage)
520 public: // internal visitor interfaces
522 template <typename T>
523 void internal_visit(backup_holder<T>& lhs_content, long) const
526 = static_cast< const backup_holder<T>* >(rhs_storage_)->get();
529 template <typename T>
530 void internal_visit(const backup_holder<T>& lhs_content, long) const
533 = static_cast< const backup_holder<T>* >(rhs_storage_)->get();
536 template <typename T>
537 void internal_visit(T& lhs_content, int) const
540 // Compile error here indicates one of variant's bounded types does
541 // not meet the requirements of the Assignable concept. Thus,
542 // variant is not Assignable.
544 // Hint: Are any of the bounded types const-qualified or references?
546 lhs_content = *static_cast< const T* >(rhs_storage_);
551 ///////////////////////////////////////////////////////////////////////////////
552 // (detail) class move_storage
554 // Internal visitor that moves the given storage (which must be a
555 // constructed value of the same type) to the value it visits.
558 : public static_visitor<>
560 private: // representation
566 explicit move_storage(void* rhs_storage) BOOST_NOEXCEPT
567 : rhs_storage_(rhs_storage)
571 public: // internal visitor interfaces
573 template <typename T>
574 void internal_visit(backup_holder<T>& lhs_content, long) const
577 = ::boost::detail::variant::move(static_cast<backup_holder<T>* >(rhs_storage_)->get());
580 template <typename T>
581 void internal_visit(const backup_holder<T>& lhs_content, long) const
584 = ::boost::detail::variant::move(static_cast<backup_holder<T>* >(rhs_storage_)->get());
587 template <typename T>
588 void internal_visit(T& lhs_content, int) const
591 // Compile error here indicates one of variant's bounded types does
592 // not meet the requirements of the Assignable concept. Thus,
593 // variant is not Assignable.
595 // Hint: Are any of the bounded types const-qualified or references?
597 lhs_content = ::boost::detail::variant::move(*static_cast<T* >(rhs_storage_));
602 ///////////////////////////////////////////////////////////////////////////////
603 // (detail) class direct_assigner
605 // Generic static visitor that: if and only if the visited value is of the
606 // specified type, assigns the given value to the visited value and returns
607 // true; else returns false.
609 template <typename T>
610 class direct_assigner
611 : public static_visitor<bool>
613 private: // representation
619 explicit direct_assigner(const T& rhs) BOOST_NOEXCEPT
624 public: // visitor interface
626 bool operator()(T& lhs)
632 template <typename U>
633 bool operator()(U&) BOOST_NOEXCEPT
638 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
640 // silence MSVC warning C4512: assignment operator could not be generated
641 direct_assigner& operator= (direct_assigner const&);
645 ///////////////////////////////////////////////////////////////////////////////
646 // (detail) class direct_mover
648 // Generic static visitor that: if and only if the visited value is of the
649 // specified type, move assigns the given value to the visited value and returns
650 // true; else returns false.
652 template <typename T>
654 : public static_visitor<bool>
656 private: // representation
662 explicit direct_mover(T& rhs) BOOST_NOEXCEPT
667 public: // visitor interface
669 bool operator()(T& lhs)
671 lhs = ::boost::detail::variant::move(rhs_);
675 template <typename U>
676 bool operator()(U&) BOOST_NOEXCEPT
681 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
683 // silence MSVC warning C4512: assignment operator could not be generated
684 direct_mover& operator= (direct_mover const&);
689 ///////////////////////////////////////////////////////////////////////////////
690 // (detail) class backup_assigner
692 // Internal visitor that "assigns" the given value to the visited value,
693 // using backup to recover if the destroy-copy sequence fails.
695 // NOTE: This needs to be a friend of variant, as it needs access to
696 // indicate_which, indicate_backup_which, etc.
698 template <typename Variant>
699 class backup_assigner
700 : public static_visitor<>
702 private: // representation
706 const void* rhs_content_;
707 void (*copy_rhs_content_)(void*, const void*);
712 backup_assigner(Variant& lhs, int rhs_which, const RhsT& rhs_content)
714 , rhs_which_(rhs_which)
715 , rhs_content_(&rhs_content)
716 , copy_rhs_content_(&construct_impl<RhsT>)
720 private: // helpers, for visitor interface (below)
723 static void construct_impl(void* addr, const void* obj)
725 new(addr) RhsT(*static_cast<const RhsT*>(obj));
728 template <typename LhsT>
729 void backup_assign_impl(
730 backup_holder<LhsT>& lhs_content
731 , mpl::false_ // is_nothrow_move_constructible
735 // Move lhs content to backup...
736 backup_holder<LhsT> backup_lhs_content(0);
737 backup_lhs_content.swap(lhs_content); // nothrow
739 // ...destroy lhs content...
740 lhs_content.~backup_holder<LhsT>(); // nothrow
744 // ...and attempt to copy rhs content into lhs storage:
745 copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
749 // In case of failure, copy backup pointer to lhs storage...
750 new(lhs_.storage_.address())
751 backup_holder<LhsT>( 0 ); // nothrow
753 static_cast<backup_holder<LhsT>* >(lhs_.storage_.address())
754 ->swap(backup_lhs_content); // nothrow
761 // In case of success, indicate new content type:
762 lhs_.indicate_which(rhs_which_); // nothrow
765 template <typename LhsT>
766 void backup_assign_impl(
768 , mpl::true_ // is_nothrow_move_constructible
772 // Move lhs content to backup...
773 LhsT backup_lhs_content(
774 ::boost::detail::variant::move(lhs_content)
777 // ...destroy lhs content...
778 lhs_content.~LhsT(); // nothrow
782 // ...and attempt to copy rhs content into lhs storage:
783 copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
787 // In case of failure, restore backup content to lhs storage...
788 new(lhs_.storage_.address())
790 ::boost::detail::variant::move(backup_lhs_content)
798 // In case of success, indicate new content type:
799 lhs_.indicate_which(rhs_which_); // nothrow
802 template <typename LhsT>
803 void backup_assign_impl(
805 , mpl::false_ // is_nothrow_move_constructible
809 // Backup lhs content...
810 LhsT* backup_lhs_ptr = new LhsT(lhs_content);
812 // ...destroy lhs content...
813 lhs_content.~LhsT(); // nothrow
817 // ...and attempt to copy rhs content into lhs storage:
818 copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
822 // In case of failure, copy backup pointer to lhs storage...
823 new(lhs_.storage_.address())
824 backup_holder<LhsT>( backup_lhs_ptr ); // nothrow
826 // ...indicate now using backup...
827 lhs_.indicate_backup_which( lhs_.which() ); // nothrow
834 // In case of success, indicate new content type...
835 lhs_.indicate_which(rhs_which_); // nothrow
837 // ...and delete backup:
838 delete backup_lhs_ptr; // nothrow
841 public: // visitor interface
843 template <typename LhsT>
844 void internal_visit(LhsT& lhs_content, int)
846 typedef typename is_nothrow_move_constructible<LhsT>::type
849 backup_assign_impl( lhs_content, nothrow_move(), 1L);
852 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
854 // silence MSVC warning C4512: assignment operator could not be generated
855 backup_assigner& operator= (backup_assigner const&);
859 ///////////////////////////////////////////////////////////////////////////////
860 // (detail) class swap_with
862 // Visitor that swaps visited value with content of given variant.
864 // Precondition: Given variant MUST have same logical type as visited value.
866 template <typename Variant>
868 : public static_visitor<>
870 private: // representation
876 explicit swap_with(Variant& toswap) BOOST_NOEXCEPT
881 public: // internal visitor interfaces
883 template <typename T>
884 void operator()(T& operand) const
886 // Since the precondition ensures types are same, get T...
888 T& other = toswap_.apply_visitor(getter);
891 ::boost::detail::variant::move_swap( operand, other );
895 swap_with& operator=(const swap_with&);
899 ///////////////////////////////////////////////////////////////////////////////
900 // (detail) class reflect
902 // Generic static visitor that performs a typeid on the value it visits.
906 : public static_visitor<const boost::typeindex::type_info&>
908 public: // visitor interfaces
910 template <typename T>
911 const boost::typeindex::type_info& operator()(const T&) const BOOST_NOEXCEPT
913 return boost::typeindex::type_id<T>().type_info();
918 ///////////////////////////////////////////////////////////////////////////////
919 // (detail) class comparer
921 // Generic static visitor that compares the content of the given lhs variant
922 // with the visited rhs content using Comp.
924 // Precondition: lhs.which() == rhs.which()
926 template <typename Variant, typename Comp>
928 : public static_visitor<bool>
930 private: // representation
936 explicit comparer(const Variant& lhs) BOOST_NOEXCEPT
941 public: // visitor interfaces
943 template <typename T>
944 bool operator()(T& rhs_content) const
946 // Since the precondition ensures lhs and rhs types are same, get T...
948 const T& lhs_content = lhs_.apply_visitor(getter);
950 // ...and compare lhs and rhs contents:
951 return Comp()(lhs_content, rhs_content);
955 comparer& operator=(const comparer&);
959 ///////////////////////////////////////////////////////////////////////////////
960 // (detail) class equal_comp
962 // Generic function object compares lhs with rhs using operator==.
966 template <typename T>
967 bool operator()(const T& lhs, const T& rhs) const
973 ///////////////////////////////////////////////////////////////////////////////
974 // (detail) class less_comp
976 // Generic function object compares lhs with rhs using operator<.
980 template <typename T>
981 bool operator()(const T& lhs, const T& rhs) const
987 ///////////////////////////////////////////////////////////////////////////////
988 // (detail) class template invoke_visitor
990 // Internal visitor that invokes the given visitor using:
991 // * for wrappers (e.g., recursive_wrapper), the wrapper's held value.
992 // * for all other values, the value itself.
994 template <typename Visitor, bool MoveSemantics>
997 private: // representation
1001 public: // visitor typedefs
1003 typedef typename Visitor::result_type
1006 public: // structors
1008 explicit invoke_visitor(Visitor& visitor) BOOST_NOEXCEPT
1013 public: // internal visitor interfaces
1015 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1017 //using workaround with is_same<T, T> to prenvent compilation error, because we need to use T in enable_if to make SFINAE work
1018 template <typename T>
1019 typename enable_if_c<MoveSemantics && is_same<T, T>::value, result_type>::type internal_visit(T&& operand, int)
1021 return visitor_(::boost::move<T>(operand));
1024 //using workaround with is_same<T, T> to prenvent compilation error, because we need to use T in enable_if to make SFINAE work
1025 template <typename T>
1026 typename disable_if_c<MoveSemantics && is_same<T, T>::value, result_type>::type internal_visit(T&& operand, int)
1028 return visitor_(operand);
1033 template <typename T>
1034 result_type internal_visit(T& operand, int)
1036 return visitor_(operand);
1039 # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564))
1040 template <typename T>
1041 result_type internal_visit(const T& operand, int)
1043 return visitor_(operand);
1047 #endif //RVALUE REFERENCES
1049 public: // internal visitor interfaces, cont.
1051 template <typename T>
1052 result_type internal_visit(boost::recursive_wrapper<T>& operand, long)
1054 return internal_visit( operand.get(), 1L );
1057 template <typename T>
1058 result_type internal_visit(const boost::recursive_wrapper<T>& operand, long)
1060 return internal_visit( operand.get(), 1L );
1063 template <typename T>
1064 result_type internal_visit(boost::detail::reference_content<T>& operand, long)
1066 return internal_visit( operand.get(), 1L );
1069 template <typename T>
1070 result_type internal_visit(const boost::detail::reference_content<T>& operand, long)
1072 return internal_visit( operand.get(), 1L );
1075 template <typename T>
1076 result_type internal_visit(boost::detail::variant::backup_holder<T>& operand, long)
1078 return internal_visit( operand.get(), 1L );
1081 template <typename T>
1082 result_type internal_visit(const boost::detail::variant::backup_holder<T>& operand, long)
1084 return internal_visit( operand.get(), 1L );
1087 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
1089 // silence MSVC warning C4512: assignment operator could not be generated
1090 invoke_visitor& operator= (invoke_visitor const&);
1094 }} // namespace detail::variant
1096 ///////////////////////////////////////////////////////////////////////////////
1097 // class template variant (concept inspired by Andrei Alexandrescu)
1099 // See docs and boost/variant/variant_fwd.hpp for more information.
1103 , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T)
1107 private: // helpers, for typedefs (below)
1109 typedef variant wknd_self_t;
1111 struct is_recursive_
1112 : detail::variant::is_recursive_flag<T0_>
1116 typedef typename mpl::eval_if<
1119 , mpl::identity< T0_ >
1120 >::type unwrapped_T0_;
1122 struct is_sequence_based_
1123 : detail::variant::is_over_sequence<unwrapped_T0_>
1127 #if !defined(BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT)
1129 private: // helpers, for typedefs (below)
1131 typedef typename mpl::eval_if<
1133 , unwrapped_T0_ // over_sequence<...>::type
1134 , detail::variant::make_variant_list<
1136 , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(T)
1138 >::type specified_types;
1140 BOOST_STATIC_ASSERT((
1141 ::boost::mpl::not_< mpl::empty<specified_types> >::value
1144 public: // public typedefs
1145 typedef typename mpl::eval_if<
1150 detail::variant::quoted_enable_recursive<wknd_self_t>
1153 , mpl::identity< specified_types >
1154 >::type recursive_enabled_types; // used by is_variant_constructible_from<> trait
1156 typedef typename mpl::transform<
1157 recursive_enabled_types
1158 , unwrap_recursive<mpl::_1>
1161 private: // internal typedefs
1163 typedef typename mpl::transform<
1164 recursive_enabled_types
1165 , mpl::protect< detail::make_reference_content<> >
1166 >::type internal_types;
1168 typedef typename mpl::front<
1170 >::type internal_T0;
1172 #else // defined(BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT)
1174 private: // helpers, for typedefs (below)
1176 typedef unwrapped_T0_ T0;
1178 #define BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS(z,N,_) \
1179 typedef typename mpl::eval_if< \
1181 , detail::variant::enable_recursive< \
1185 , mpl::identity< BOOST_PP_CAT(T,N) > \
1186 >::type BOOST_PP_CAT(recursive_enabled_T,N); \
1190 BOOST_VARIANT_LIMIT_TYPES
1191 , BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS
1195 #undef BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS
1197 #define BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS(z,N,_) \
1198 typedef typename unwrap_recursive< \
1199 BOOST_PP_CAT(recursive_enabled_T,N) \
1200 >::type BOOST_PP_CAT(public_T,N); \
1204 BOOST_VARIANT_LIMIT_TYPES
1205 , BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS
1209 #undef BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS
1211 public: // public typedefs
1213 typedef typename detail::variant::make_variant_list<
1214 BOOST_VARIANT_ENUM_PARAMS(public_T)
1217 private: // helpers, for internal typedefs (below)
1219 #define BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS(z,N,_) \
1220 typedef detail::make_reference_content< \
1221 BOOST_PP_CAT(recursive_enabled_T,N) \
1222 >::type BOOST_PP_CAT(internal_T,N); \
1226 BOOST_VARIANT_LIMIT_TYPES
1227 , BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS
1231 #undef BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS
1233 private: // internal typedefs
1235 typedef typename detail::variant::make_variant_list<
1236 BOOST_VARIANT_ENUM_PARAMS(internal_T)
1237 >::type internal_types;
1239 private: // static precondition assertions
1242 // variant< type-sequence > syntax is not supported on this compiler!
1244 BOOST_MPL_ASSERT_NOT(( is_sequence_based_ ));
1246 #endif // BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT workaround
1248 private: // helpers, for representation (below)
1250 typedef typename detail::variant::find_fallback_type<
1252 >::type fallback_type_result_;
1254 typedef typename fallback_type_result_::first
1255 fallback_type_index_;
1256 typedef typename fallback_type_result_::second
1259 struct has_fallback_type_
1261 is_same< fallback_type_, detail::variant::no_fallback_type >
1266 typedef has_fallback_type_
1267 never_uses_backup_flag;
1269 typedef typename detail::variant::make_storage<
1270 internal_types, never_uses_backup_flag
1273 #ifndef BOOST_NO_CXX11_NOEXCEPT
1274 typedef typename detail::variant::is_variant_move_noexcept_constructible<
1276 > variant_move_noexcept_constructible;
1278 typedef typename detail::variant::is_variant_move_noexcept_assignable<
1280 > variant_move_noexcept_assignable;
1284 private: // helpers, for representation (below)
1287 // * [0, size<internal_types>) indicates stack content
1288 // * [-size<internal_types>, 0) indicates pointer to heap backup
1290 // * then which() -> which_
1291 // * else which() -> -(which_ + 1)
1293 #if !defined(BOOST_VARIANT_MINIMIZE_SIZE)
1295 typedef int which_t;
1297 #else // defined(BOOST_VARIANT_MINIMIZE_SIZE)
1299 // [if O1_size available, then attempt which_t size optimization...]
1300 // [select signed char if fewer than SCHAR_MAX types, else signed int:]
1301 typedef typename mpl::eval_if<
1302 mpl::equal_to< mpl::O1_size<internal_types>, mpl::long_<-1> >
1303 , mpl::identity< int >
1305 mpl::less< mpl::O1_size<internal_types>, mpl::int_<SCHAR_MAX> >
1311 #endif // BOOST_VARIANT_MINIMIZE_SIZE switch
1313 // representation -- private when possible
1314 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
1323 void indicate_which(int which_arg) BOOST_NOEXCEPT
1325 which_ = static_cast<which_t>( which_arg );
1328 void indicate_backup_which(int which_arg) BOOST_NOEXCEPT
1330 which_ = static_cast<which_t>( -(which_arg + 1) );
1333 private: // helpers, for queries (below)
1335 bool using_backup() const BOOST_NOEXCEPT
1342 int which() const BOOST_NOEXCEPT
1344 // If using heap backup...
1346 // ...then return adjusted which_:
1347 return -(which_ + 1);
1349 // Otherwise, return which_ directly:
1353 private: // helpers, for structors (below)
1356 : BOOST_VARIANT_AUX_INITIALIZER_T(
1357 recursive_enabled_types, recursive_enabled_T
1362 void destroy_content() BOOST_NOEXCEPT
1364 detail::variant::destroyer visitor;
1365 this->internal_apply_visitor(visitor);
1368 public: // structors
1370 ~variant() BOOST_NOEXCEPT
1376 #if !(defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5130))
1377 BOOST_NOEXCEPT_IF(boost::has_nothrow_constructor<internal_T0>::value)
1381 #pragma warning( push )
1382 // behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
1383 #pragma warning( disable : 4345 )
1386 // Compile error from here indicates that the first bound
1387 // type is not default-constructible, and so variant cannot
1388 // support its own default-construction.
1390 new( storage_.address() ) internal_T0();
1391 indicate_which(0); // zero is the index of the first bounded type
1393 #pragma warning( pop )
1397 private: // helpers, for structors, cont. (below)
1399 class convert_copy_into
1400 : public static_visitor<int>
1402 private: // representation
1406 public: // structors
1408 explicit convert_copy_into(void* storage) BOOST_NOEXCEPT
1413 public: // internal visitor interfaces (below)
1415 template <typename T>
1416 int internal_visit(T& operand, int) const
1419 // Compile error here indicates one of the source variant's types
1420 // cannot be unambiguously converted to the destination variant's
1421 // types (or that no conversion exists).
1423 return initializer::initialize(storage_, operand);
1426 # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564))
1427 template <typename T>
1428 result_type internal_visit(const T& operand, int) const
1430 return initializer::initialize(storage_, operand);
1434 template <typename T>
1435 int internal_visit(boost::detail::reference_content<T>& operand, long) const
1437 return internal_visit( operand.get(), 1L );
1440 template <typename T>
1441 int internal_visit(const boost::detail::reference_content<T>& operand, long) const
1443 return internal_visit( operand.get(), 1L );
1446 template <typename T>
1447 int internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
1449 return internal_visit( operand.get(), 1L );
1452 template <typename T>
1453 int internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
1455 return internal_visit( operand.get(), 1L );
1458 template <typename T>
1459 int internal_visit(boost::recursive_wrapper<T>& operand, long) const
1461 return internal_visit( operand.get(), 1L );
1464 template <typename T>
1465 int internal_visit(const boost::recursive_wrapper<T>& operand, long) const
1467 return internal_visit( operand.get(), 1L );
1472 friend class convert_copy_into;
1474 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1475 class convert_move_into
1476 : public static_visitor<int>
1478 private: // representation
1482 public: // structors
1484 explicit convert_move_into(void* storage) BOOST_NOEXCEPT
1489 public: // internal visitor interfaces (below)
1491 template <typename T>
1492 int internal_visit(T& operand, int) const
1495 // Compile error here indicates one of the source variant's types
1496 // cannot be unambiguously converted to the destination variant's
1497 // types (or that no conversion exists).
1499 return initializer::initialize(storage_, detail::variant::move(operand) );
1502 template <typename T>
1503 int internal_visit(boost::detail::reference_content<T>& operand, long) const
1505 return internal_visit( operand.get(), 1L );
1508 template <typename T>
1509 int internal_visit(const boost::detail::reference_content<T>& operand, long) const
1511 return internal_visit( operand.get(), 1L );
1514 template <typename T>
1515 int internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
1517 return internal_visit( operand.get(), 1L );
1520 template <typename T>
1521 int internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
1523 return internal_visit( operand.get(), 1L );
1526 template <typename T>
1527 int internal_visit(boost::recursive_wrapper<T>& operand, long) const
1529 return internal_visit( operand.get(), 1L );
1532 template <typename T>
1533 int internal_visit(const boost::recursive_wrapper<T>& operand, long) const
1535 return internal_visit( operand.get(), 1L );
1539 friend class convert_move_into;
1542 private: // helpers, for structors, below
1544 template <typename T>
1545 void convert_construct(
1548 , mpl::false_ = mpl::false_() // is_foreign_variant
1552 // Compile error here indicates that the given type is not
1553 // unambiguously convertible to one of the variant's types
1554 // (or that no conversion exists).
1557 initializer::initialize(
1564 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1565 template <typename T>
1566 typename boost::enable_if<boost::is_rvalue_reference<T&&> >::type convert_construct(
1569 , mpl::false_ = mpl::false_() // is_foreign_variant
1573 // Compile error here indicates that the given type is not
1574 // unambiguously convertible to one of the variant's types
1575 // (or that no conversion exists).
1578 initializer::initialize(
1580 , detail::variant::move(operand)
1586 template <typename Variant>
1587 void convert_construct(
1590 , mpl::true_// is_foreign_variant
1593 convert_copy_into visitor(storage_.address());
1595 operand.internal_apply_visitor(visitor)
1599 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1600 template <typename Variant>
1601 typename boost::enable_if<boost::is_rvalue_reference<Variant&&> >::type convert_construct(
1604 , mpl::true_// is_foreign_variant
1607 convert_move_into visitor(storage_.address());
1609 operand.internal_apply_visitor(visitor)
1614 template <typename Variant>
1615 void convert_construct_variant(Variant& operand)
1617 // [Determine if the given variant is itself a bounded type, or if its
1618 // content needs to be converted (i.e., it is a 'foreign' variant):]
1621 typedef typename mpl::find_if<
1629 typedef typename mpl::end<types>::type not_found;
1630 typedef typename is_same<
1632 >::type is_foreign_variant;
1634 // Convert construct from operand:
1637 , is_foreign_variant()
1641 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1642 template <typename Variant>
1643 typename boost::enable_if<boost::is_rvalue_reference<Variant&&> >::type convert_construct_variant(Variant&& operand)
1645 // [Determine if the given variant is itself a bounded type, or if its
1646 // content needs to be converted (i.e., it is a 'foreign' variant):]
1649 typedef typename mpl::find_if<
1657 typedef typename mpl::end<types>::type not_found;
1658 typedef typename is_same<
1660 >::type is_foreign_variant;
1662 // Convert move construct from operand:
1664 detail::variant::move(operand), 1L
1665 , is_foreign_variant()
1670 template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
1671 typename boost::enable_if<mpl::or_<
1672 boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
1673 boost::detail::variant::is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&, internal_types>
1674 > >::type convert_construct(
1675 boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>& operand
1679 convert_construct_variant(operand);
1682 template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
1683 typename boost::enable_if<mpl::or_<
1684 boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
1685 boost::detail::variant::is_variant_constructible_from<const boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&, internal_types>
1686 > >::type convert_construct(
1687 const boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>& operand
1691 convert_construct_variant(operand);
1694 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1695 template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
1696 typename boost::enable_if<mpl::or_<
1697 boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
1698 boost::detail::variant::is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&&, internal_types>
1699 > >::type convert_construct(
1700 boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&& operand
1704 convert_construct_variant( detail::variant::move(operand) );
1708 public: // structors, cont.
1710 template <typename T>
1711 variant(const T& operand,
1712 typename boost::enable_if<mpl::or_<
1714 mpl::not_< boost::is_same<T, variant> >,
1715 boost::detail::variant::is_variant_constructible_from<const T&, internal_types>
1717 boost::is_same<T, boost::recursive_variant_> > >::type* = 0)
1719 convert_construct(operand, 1L);
1722 template <typename T>
1725 , typename boost::enable_if<mpl::or_<
1727 mpl::not_< is_const<T> >,
1728 mpl::not_< boost::is_same<T, variant> >,
1729 boost::detail::variant::is_variant_constructible_from<T&, internal_types>
1731 boost::is_same<T, boost::recursive_variant_> > >::type* = 0
1734 convert_construct(operand, 1L);
1737 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1739 variant(T&& operand,
1740 typename boost::enable_if<mpl::or_<
1742 boost::is_rvalue_reference<T&&>,
1743 mpl::not_< boost::is_const<T> >,
1744 mpl::not_< boost::is_same<T, variant> >,
1745 boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
1747 boost::is_same<T, boost::recursive_variant_> > >::type* = 0)
1749 convert_construct( detail::variant::move(operand), 1L);
1753 public: // structors, cont.
1755 // [MSVC6 requires copy constructor appear after template constructors]
1756 variant(const variant& operand)
1758 // Copy the value of operand into *this...
1759 detail::variant::copy_into visitor( storage_.address() );
1760 operand.internal_apply_visitor(visitor);
1762 // ...and activate the *this's primary storage on success:
1763 indicate_which(operand.which());
1766 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1767 variant(variant&& operand) BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value)
1769 // Move the value of operand into *this...
1770 detail::variant::move_into visitor( storage_.address() );
1771 operand.internal_apply_visitor(visitor);
1773 // ...and activate the *this's primary storage on success:
1774 indicate_which(operand.which());
1778 private: // helpers, for modifiers (below)
1780 # if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
1781 template <typename Variant>
1782 friend class detail::variant::backup_assigner;
1787 // Internal visitor that "assigns" the visited value to the given variant
1788 // by appropriate destruction and copy-construction.
1792 : public static_visitor<>
1794 protected: // representation
1797 const int rhs_which_;
1799 public: // structors
1801 assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
1803 , rhs_which_(rhs_which)
1807 protected: // helpers, for internal visitor interface (below)
1809 template <typename RhsT, typename B1, typename B2>
1811 const RhsT& rhs_content
1812 , mpl::true_ // has_nothrow_copy
1813 , B1 // is_nothrow_move_constructible
1814 , B2 // has_fallback_type
1815 ) const BOOST_NOEXCEPT
1817 // Destroy lhs's content...
1818 lhs_.destroy_content(); // nothrow
1820 // ...copy rhs content into lhs's storage...
1821 new(lhs_.storage_.address())
1822 RhsT( rhs_content ); // nothrow
1824 // ...and indicate new content type:
1825 lhs_.indicate_which(rhs_which_); // nothrow
1828 template <typename RhsT, typename B>
1830 const RhsT& rhs_content
1831 , mpl::false_ // has_nothrow_copy
1832 , mpl::true_ // is_nothrow_move_constructible
1833 , B // has_fallback_type
1836 // Attempt to make a temporary copy (so as to move it below)...
1837 RhsT temp(rhs_content);
1839 // ...and upon success destroy lhs's content...
1840 lhs_.destroy_content(); // nothrow
1842 // ...move the temporary copy into lhs's storage...
1843 new(lhs_.storage_.address())
1844 RhsT( detail::variant::move(temp) ); // nothrow
1846 // ...and indicate new content type:
1847 lhs_.indicate_which(rhs_which_); // nothrow
1850 void construct_fallback() const BOOST_NOEXCEPT {
1851 // In case of failure, default-construct fallback type in lhs's storage...
1852 new (lhs_.storage_.address())
1853 fallback_type_; // nothrow
1855 // ...indicate construction of fallback type...
1856 lhs_.indicate_which(
1857 BOOST_MPL_AUX_VALUE_WKND(fallback_type_index_)::value
1861 template <typename RhsT>
1863 const RhsT& rhs_content
1864 , mpl::false_ // has_nothrow_copy
1865 , mpl::false_ // is_nothrow_move_constructible
1866 , mpl::true_ // has_fallback_type
1869 // Destroy lhs's content...
1870 lhs_.destroy_content(); // nothrow
1874 // ...and attempt to copy rhs's content into lhs's storage:
1875 new(lhs_.storage_.address())
1876 RhsT( rhs_content );
1880 construct_fallback();
1887 // In the event of success, indicate new content type:
1888 lhs_.indicate_which(rhs_which_); // nothrow
1891 template <typename RhsT>
1893 const RhsT& rhs_content
1894 , mpl::false_ // has_nothrow_copy
1895 , mpl::false_ // is_nothrow_move_constructible
1896 , mpl::false_ // has_fallback_type
1899 detail::variant::backup_assigner<wknd_self_t>
1900 visitor(lhs_, rhs_which_, rhs_content);
1901 lhs_.internal_apply_visitor(visitor);
1904 public: // internal visitor interfaces
1906 template <typename RhsT>
1907 void internal_visit(const RhsT& rhs_content, int) const
1909 typedef typename has_nothrow_copy<RhsT>::type
1911 typedef typename mpl::or_< // reduces compile-time
1913 , is_nothrow_move_constructible<RhsT>
1914 >::type nothrow_move_constructor;
1919 , nothrow_move_constructor()
1920 , has_fallback_type_()
1924 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
1926 // silence MSVC warning C4512: assignment operator could not be generated
1927 assigner& operator= (assigner const&);
1931 friend class assigner;
1933 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
1934 // class move_assigner
1936 // Internal visitor that "move assigns" the visited value to the given variant
1937 // by appropriate destruction and move-construction.
1943 public: // structors
1945 move_assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
1946 : assigner(lhs, rhs_which)
1950 private: // helpers, for internal visitor interface (below)
1952 template <typename RhsT, typename B2>
1955 , mpl::true_ // has_nothrow_copy
1956 , mpl::false_ // is_nothrow_move_constructible
1957 , B2 // has_fallback_type
1958 ) const BOOST_NOEXCEPT
1960 assigner::assign_impl(rhs_content, mpl::true_(), mpl::false_(), B2());
1963 template <typename RhsT, typename B, typename B2>
1966 , B // has_nothrow_copy
1967 , mpl::true_ // is_nothrow_move_constructible
1968 , B2 // has_fallback_type
1969 ) const BOOST_NOEXCEPT
1971 // ...destroy lhs's content...
1972 assigner::lhs_.destroy_content(); // nothrow
1974 // ...move the rhs_content into lhs's storage...
1975 new(assigner::lhs_.storage_.address())
1976 RhsT( detail::variant::move(rhs_content) ); // nothrow
1978 // ...and indicate new content type:
1979 assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
1982 template <typename RhsT>
1985 , mpl::false_ // has_nothrow_copy
1986 , mpl::false_ // is_nothrow_move_constructible
1987 , mpl::true_ // has_fallback_type
1990 // Destroy lhs's content...
1991 assigner::lhs_.destroy_content(); // nothrow
1995 // ...and attempt to copy rhs's content into lhs's storage:
1996 new(assigner::lhs_.storage_.address())
1997 RhsT( detail::variant::move(rhs_content) );
2001 assigner::construct_fallback();
2008 // In the event of success, indicate new content type:
2009 assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
2012 template <typename RhsT>
2015 , mpl::false_ // has_nothrow_copy
2016 , mpl::false_ // is_nothrow_move_constructible
2017 , mpl::false_ // has_fallback_type
2020 assigner::assign_impl(rhs_content, mpl::false_(), mpl::false_(), mpl::false_());
2023 public: // internal visitor interfaces
2025 template <typename RhsT>
2026 void internal_visit(RhsT& rhs_content, int) const
2028 typedef typename is_nothrow_move_constructible<RhsT>::type
2029 nothrow_move_constructor;
2030 typedef typename mpl::or_< // reduces compile-time
2031 nothrow_move_constructor
2032 , has_nothrow_copy<RhsT>
2033 >::type nothrow_copy;
2038 , nothrow_move_constructor()
2039 , has_fallback_type_()
2043 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
2045 // silence MSVC warning C4512: assignment operator could not be generated
2046 move_assigner& operator= (move_assigner const&);
2050 friend class move_assigner;
2051 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
2053 void variant_assign(const variant& rhs)
2055 // If the contained types are EXACTLY the same...
2056 if (which_ == rhs.which_)
2058 // ...then assign rhs's storage to lhs's content:
2059 detail::variant::assign_storage visitor(rhs.storage_.address());
2060 this->internal_apply_visitor(visitor);
2064 // Otherwise, perform general (copy-based) variant assignment:
2065 assigner visitor(*this, rhs.which());
2066 rhs.internal_apply_visitor(visitor);
2070 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
2071 void variant_assign(variant&& rhs)
2073 // If the contained types are EXACTLY the same...
2074 if (which_ == rhs.which_)
2076 // ...then move rhs's storage to lhs's content:
2077 detail::variant::move_storage visitor(rhs.storage_.address());
2078 this->internal_apply_visitor(visitor);
2082 // Otherwise, perform general (move-based) variant assignment:
2083 move_assigner visitor(*this, rhs.which());
2084 rhs.internal_apply_visitor(visitor);
2087 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
2089 private: // helpers, for modifiers (below)
2091 template <typename T>
2092 void assign(const T& rhs)
2094 // If direct T-to-T assignment is not possible...
2095 detail::variant::direct_assigner<T> direct_assign(rhs);
2096 if (this->apply_visitor(direct_assign) == false)
2098 // ...then convert rhs to variant and assign:
2100 // While potentially inefficient, the following construction of a
2101 // variant allows T as any type convertible to one of the bounded
2102 // types without excessive code redundancy.
2105 variant_assign( detail::variant::move(temp) );
2109 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
2110 template <typename T>
2111 void move_assign(T&& rhs)
2113 // If direct T-to-T move assignment is not possible...
2114 detail::variant::direct_mover<T> direct_move(rhs);
2115 if (this->apply_visitor(direct_move) == false)
2117 // ...then convert rhs to variant and assign:
2119 // While potentially inefficient, the following construction of a
2120 // variant allows T as any type convertible to one of the bounded
2121 // types without excessive code redundancy.
2123 variant temp( detail::variant::move(rhs) );
2124 variant_assign( detail::variant::move(temp) );
2127 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
2129 public: // modifiers
2131 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
2133 typename boost::enable_if<
2135 boost::is_rvalue_reference<T&&>,
2136 mpl::not_< boost::is_const<T> >,
2137 boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
2140 >::type operator=(T&& rhs)
2142 move_assign( detail::variant::move(rhs) );
2145 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
2147 template <typename T>
2148 typename boost::enable_if<
2150 boost::is_same<T, variant>,
2151 boost::detail::variant::is_variant_constructible_from<const T&, internal_types>
2154 >::type operator=(const T& rhs)
2160 // [MSVC6 requires copy assign appear after templated operator=]
2161 variant& operator=(const variant& rhs)
2163 variant_assign(rhs);
2167 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
2168 variant& operator=(variant&& rhs)
2169 #if !defined(__GNUC__) || (__GNUC__ != 4) || (__GNUC_MINOR__ > 6) || defined(__clang__)
2170 BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value && variant_move_noexcept_assignable::type::value)
2173 variant_assign( detail::variant::move(rhs) );
2176 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
2178 void swap(variant& rhs)
2180 // If the contained types are the same...
2181 if (which() == rhs.which())
2183 // ...then swap the values directly:
2184 detail::variant::swap_with<variant> visitor(rhs);
2185 this->apply_visitor(visitor);
2189 // ...otherwise, perform general variant swap:
2190 variant tmp( detail::variant::move(rhs) );
2191 rhs = detail::variant::move(*this);
2192 *this = detail::variant::move(tmp);
2199 // NOTE: member which() defined above.
2202 bool empty() const BOOST_NOEXCEPT
2207 const boost::typeindex::type_info& type() const
2209 detail::variant::reflect visitor;
2210 return this->apply_visitor(visitor);
2213 public: // prevent comparison with foreign types
2215 template <typename U>
2216 void operator==(const U&) const
2218 BOOST_STATIC_ASSERT( false && sizeof(U) );
2221 template <typename U>
2222 void operator<(const U&) const
2224 BOOST_STATIC_ASSERT( false && sizeof(U) );
2227 template <typename U>
2228 void operator!=(const U&) const
2230 BOOST_STATIC_ASSERT( false && sizeof(U) );
2233 template <typename U>
2234 void operator>(const U&) const
2236 BOOST_STATIC_ASSERT( false && sizeof(U) );
2239 template <typename U>
2240 void operator<=(const U&) const
2242 BOOST_STATIC_ASSERT( false && sizeof(U) );
2245 template <typename U>
2246 void operator>=(const U&) const
2248 BOOST_STATIC_ASSERT( false && sizeof(U) );
2251 public: // comparison operators
2253 // [MSVC6 requires these operators appear after template operators]
2255 bool operator==(const variant& rhs) const
2257 if (this->which() != rhs.which())
2260 detail::variant::comparer<
2261 variant, detail::variant::equal_comp
2263 return rhs.apply_visitor(visitor);
2266 bool operator<(const variant& rhs) const
2269 // Dirk Schreib suggested this collating order.
2272 if (this->which() != rhs.which())
2273 return this->which() < rhs.which();
2275 detail::variant::comparer<
2276 variant, detail::variant::less_comp
2278 return rhs.apply_visitor(visitor);
2281 ///////////////////////////////////////////////////////////////////////////////
2282 // comparison operators != > <= >=
2283 inline bool operator!=(const variant& rhs) const
2285 return !(*this == rhs);
2288 inline bool operator>(const variant& rhs) const
2293 inline bool operator<=(const variant& rhs) const
2295 return !(*this > rhs);
2298 inline bool operator>=(const variant& rhs) const
2300 return !(*this < rhs);
2303 // helpers, for visitation support (below) -- private when possible
2304 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
2306 template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
2307 friend class variant;
2311 #else// defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
2315 #endif// !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
2317 template <typename Visitor, typename VoidPtrCV>
2318 BOOST_FORCEINLINE static typename Visitor::result_type
2319 internal_apply_visitor_impl(
2326 typedef mpl::int_<0> first_which;
2327 typedef typename mpl::begin<internal_types>::type first_it;
2328 typedef typename mpl::end<internal_types>::type last_it;
2330 typedef detail::variant::visitation_impl_step<
2334 return detail::variant::visitation_impl(
2335 internal_which, logical_which
2336 , visitor, storage, mpl::false_()
2337 , never_uses_backup_flag()
2338 , static_cast<first_which*>(0), static_cast<first_step*>(0)
2342 template <typename Visitor>
2343 BOOST_FORCEINLINE typename Visitor::result_type
2344 internal_apply_visitor(Visitor& visitor)
2346 return internal_apply_visitor_impl(
2347 which_, which(), visitor, storage_.address()
2351 template <typename Visitor>
2352 BOOST_FORCEINLINE typename Visitor::result_type
2353 internal_apply_visitor(Visitor& visitor) const
2355 return internal_apply_visitor_impl(
2356 which_, which(), visitor, storage_.address()
2360 public: // visitation support
2362 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
2364 template <typename Visitor>
2365 typename Visitor::result_type
2366 apply_visitor(Visitor& visitor) &&
2368 detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
2369 return this->internal_apply_visitor(invoker);
2372 template <typename Visitor>
2373 typename Visitor::result_type
2374 apply_visitor(Visitor& visitor) const&&
2376 detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
2377 return this->internal_apply_visitor(invoker);
2382 template <typename Visitor>
2383 typename Visitor::result_type
2384 apply_visitor(Visitor& visitor)
2385 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
2389 detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
2390 return this->internal_apply_visitor(invoker);
2393 template <typename Visitor>
2394 typename Visitor::result_type
2395 apply_visitor(Visitor& visitor) const
2396 #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
2400 detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
2401 return this->internal_apply_visitor(invoker);
2406 ///////////////////////////////////////////////////////////////////////////////
2407 // metafunction make_variant_over
2409 // See docs and boost/variant/variant_fwd.hpp for more information.
2411 template <typename Types>
2412 struct make_variant_over
2414 private: // precondition assertions
2416 BOOST_STATIC_ASSERT(( ::boost::mpl::is_sequence<Types>::value ));
2417 typedef typename boost::mpl::insert_range<
2419 , boost::mpl::end< boost::mpl::list<> >::type
2421 >::type copied_sequence_t;
2423 public: // metafunction result
2426 detail::variant::over_sequence<copied_sequence_t>
2431 ///////////////////////////////////////////////////////////////////////////////
2432 // function template swap
2434 // Swaps two variants of the same type (i.e., identical specification).
2436 template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
2438 variant< BOOST_VARIANT_ENUM_PARAMS(T) >& lhs
2439 , variant< BOOST_VARIANT_ENUM_PARAMS(T) >& rhs
2445 } // namespace boost
2447 // implementation additions
2449 #if !defined(BOOST_NO_IOSTREAM)
2450 #include <boost/variant/detail/variant_io.hpp>
2451 #endif // BOOST_NO_IOSTREAM
2453 #endif // BOOST_VARIANT_VARIANT_HPP