#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED #define BOOST_VARIANT2_VARIANT_HPP_INCLUDED // Copyright 2017-2019 Peter Dimov. // // Distributed under the Boost Software License, Version 1.0. // // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt #if defined(_MSC_VER) && _MSC_VER < 1910 # pragma warning( push ) # pragma warning( disable: 4521 4522 ) // multiple copy operators #endif #include #include #include #include #include #include #include #include #include #include #include // std::hash #include #include // namespace boost { #ifdef BOOST_NO_EXCEPTIONS BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined #endif template struct hash; namespace variant2 { // bad_variant_access class bad_variant_access: public std::exception { public: bad_variant_access() noexcept { } char const * what() const noexcept { return "bad_variant_access"; } }; namespace detail { BOOST_NORETURN inline void throw_bad_variant_access() { #ifdef BOOST_NO_EXCEPTIONS boost::throw_exception( bad_variant_access() ); #else throw bad_variant_access(); #endif } } // namespace detail // monostate struct monostate { }; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1940) constexpr bool operator<(monostate, monostate) noexcept { return false; } constexpr bool operator>(monostate, monostate) noexcept { return false; } constexpr bool operator<=(monostate, monostate) noexcept { return true; } constexpr bool operator>=(monostate, monostate) noexcept { return true; } constexpr bool operator==(monostate, monostate) noexcept { return true; } constexpr bool operator!=(monostate, monostate) noexcept { return false; } #else constexpr bool operator<(monostate const&, monostate const&) noexcept { return false; } constexpr bool operator>(monostate const&, monostate const&) noexcept { return false; } constexpr bool operator<=(monostate const&, monostate const&) noexcept { return true; } constexpr bool operator>=(monostate const&, monostate const&) noexcept { return true; } constexpr bool operator==(monostate const&, monostate const&) noexcept { return true; } constexpr bool operator!=(monostate const&, monostate const&) noexcept { return false; } #endif // variant forward declaration template class variant; // variant_size template struct variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; template struct variant_size: variant_size { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template /*inline*/ constexpr std::size_t variant_size_v = variant_size::value; #endif template struct variant_size>: mp11::mp_size> { }; // variant_alternative template struct variant_alternative; template using variant_alternative_t = typename variant_alternative::type; #if BOOST_WORKAROUND(BOOST_GCC, < 40900) namespace detail { template struct variant_alternative_impl { }; template struct variant_alternative_impl, true> { using type = mp11::mp_at_c, I>; }; template struct variant_alternative_impl const, true>: std::add_const< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl volatile, true>: std::add_volatile< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl const volatile, true>: std::add_cv< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl const&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> const > { }; template struct variant_alternative_impl volatile&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> volatile > { }; template struct variant_alternative_impl const volatile&, true>: std::add_lvalue_reference< mp11::mp_at_c, I> const volatile > { }; template struct variant_alternative_impl&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> > { }; template struct variant_alternative_impl const&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> const > { }; template struct variant_alternative_impl volatile&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> volatile > { }; template struct variant_alternative_impl const volatile&&, true>: std::add_rvalue_reference< mp11::mp_at_c, I> const volatile > { }; } // namespace detail template struct variant_alternative { }; template struct variant_alternative>: public detail::variant_alternative_impl, (I < sizeof...(T))> { }; template struct variant_alternative const>: public detail::variant_alternative_impl const, (I < sizeof...(T))> { }; template struct variant_alternative volatile>: public detail::variant_alternative_impl volatile, (I < sizeof...(T))> { }; template struct variant_alternative const volatile>: public detail::variant_alternative_impl const volatile, (I < sizeof...(T))> { }; template struct variant_alternative&>: public detail::variant_alternative_impl&, (I < sizeof...(T))> { }; template struct variant_alternative const&>: public detail::variant_alternative_impl const&, (I < sizeof...(T))> { }; template struct variant_alternative volatile&>: public detail::variant_alternative_impl volatile&, (I < sizeof...(T))> { }; template struct variant_alternative const volatile&>: public detail::variant_alternative_impl const volatile&, (I < sizeof...(T))> { }; template struct variant_alternative&&>: public detail::variant_alternative_impl&&, (I < sizeof...(T))> { }; template struct variant_alternative const&&>: public detail::variant_alternative_impl const&&, (I < sizeof...(T))> { }; template struct variant_alternative volatile&&>: public detail::variant_alternative_impl volatile&&, (I < sizeof...(T))> { }; template struct variant_alternative const volatile&&>: public detail::variant_alternative_impl const volatile&&, (I < sizeof...(T))> { }; #else namespace detail { #if defined( BOOST_MP11_VERSION ) && BOOST_MP11_VERSION >= 107000 template using var_alt_impl = mp11::mp_invoke_q>; #else template using var_alt_impl = mp11::mp_invoke>; #endif } // namespace detail template struct variant_alternative { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative: mp11::mp_defer, T, mp11::mp_quote_trait> { }; template struct variant_alternative>: mp11::mp_defer, mp11::mp_size_t> { }; #endif // variant_npos constexpr std::size_t variant_npos = ~static_cast( 0 ); // holds_alternative template constexpr bool holds_alternative( variant const& v ) noexcept { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); return v.index() == mp11::mp_find, U>::value; } // get (index) template constexpr variant_alternative_t>& get(variant& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t>&& get(variant&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t() ) ); #else if( v.index() != I ) detail::throw_bad_variant_access(); return std::move( v._get_impl( mp11::mp_size_t() ) ); #endif } template constexpr variant_alternative_t> const& get(variant const& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t> const&& get(variant const&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t() ) ); #else if( v.index() != I ) detail::throw_bad_variant_access(); return std::move( v._get_impl( mp11::mp_size_t() ) ); #endif } // unsafe_get #if !defined(BOOST_NO_CXX14_CONSTEXPR) # define BOOST_VARIANT2_CX14_ASSERT(expr) BOOST_ASSERT(expr); #else # define BOOST_VARIANT2_CX14_ASSERT(expr) #endif template constexpr variant_alternative_t>& unsafe_get(variant& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); BOOST_VARIANT2_CX14_ASSERT( v.index() == I ) return v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t>&& unsafe_get(variant&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); BOOST_VARIANT2_CX14_ASSERT( v.index() == I ) return std::move( v._get_impl( mp11::mp_size_t() ) ); } template constexpr variant_alternative_t> const& unsafe_get(variant const& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); BOOST_VARIANT2_CX14_ASSERT( v.index() == I ) return v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t> const&& unsafe_get(variant const&& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); BOOST_VARIANT2_CX14_ASSERT( v.index() == I ) return std::move( v._get_impl( mp11::mp_size_t() ) ); } // get (type) template constexpr U& get(variant& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() ); } template constexpr U&& get(variant&& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) ); #else if( v.index() != I::value ) detail::throw_bad_variant_access(); return std::move( v._get_impl( I() ) ); #endif } template constexpr U const& get(variant const& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() ); } template constexpr U const&& get(variant const&& v) { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) ); #else if( v.index() != I::value ) detail::throw_bad_variant_access(); return std::move( v._get_impl( I() ) ); #endif } // get_if template constexpr typename std::add_pointer>>::type get_if(variant* v) noexcept { static_assert( I < sizeof...(T), "Index out of bounds" ); return v && v->index() == I? &v->_get_impl( mp11::mp_size_t() ): 0; } template constexpr typename std::add_pointer>>::type get_if(variant const * v) noexcept { static_assert( I < sizeof...(T), "Index out of bounds" ); return v && v->index() == I? &v->_get_impl( mp11::mp_size_t() ): 0; } template constexpr typename std::add_pointer::type get_if(variant* v) noexcept { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return v && v->index() == I::value? &v->_get_impl( I() ): 0; } template constexpr typename std::add_pointer::type get_if(variant const * v) noexcept { static_assert( mp11::mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); using I = mp11::mp_find, U>; return v && v->index() == I::value? &v->_get_impl( I() ): 0; } // namespace detail { // trivially_* #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 template struct is_trivially_copy_constructible: mp11::mp_bool::value && std::has_trivial_copy_constructor::value> { }; template struct is_trivially_copy_assignable: mp11::mp_bool::value && std::has_trivial_copy_assign::value> { }; template struct is_trivially_move_constructible: mp11::mp_bool::value && std::is_trivial::value> { }; template struct is_trivially_move_assignable: mp11::mp_bool::value && std::is_trivial::value> { }; #else using std::is_trivially_copy_constructible; using std::is_trivially_copy_assignable; using std::is_trivially_move_constructible; using std::is_trivially_move_assignable; #endif // variant_storage template union variant_storage_impl; template using variant_storage = variant_storage_impl...>, T...>; template union variant_storage_impl { }; // not all trivially destructible template union variant_storage_impl { T1 first_; variant_storage rest_; template constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward(a)... ) { } template constexpr variant_storage_impl( mp11::mp_size_t, A&&... a ): rest_( mp11::mp_size_t(), std::forward(a)... ) { } ~variant_storage_impl() { } template void emplace( mp11::mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } template void emplace( mp11::mp_size_t, A&&... a ) { rest_.emplace( mp11::mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I-1>& get( mp11::mp_size_t ) noexcept { return rest_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I-1> const& get( mp11::mp_size_t ) const noexcept { return rest_.get( mp11::mp_size_t() ); } }; template union variant_storage_impl { T0 t0_; T1 t1_; T2 t2_; T3 t3_; T4 t4_; T5 t5_; T6 t6_; T7 t7_; T8 t8_; T9 t9_; variant_storage rest_; template constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): t0_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<1>, A&&... a ): t1_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<2>, A&&... a ): t2_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<3>, A&&... a ): t3_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<4>, A&&... a ): t4_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<5>, A&&... a ): t5_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<6>, A&&... a ): t6_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<7>, A&&... a ): t7_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<8>, A&&... a ): t8_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<9>, A&&... a ): t9_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t, A&&... a ): rest_( mp11::mp_size_t(), std::forward(a)... ) {} ~variant_storage_impl() { } template void emplace( mp11::mp_size_t<0>, A&&... a ) { ::new( &t0_ ) T0( std::forward(a)... ); } template void emplace( mp11::mp_size_t<1>, A&&... a ) { ::new( &t1_ ) T1( std::forward(a)... ); } template void emplace( mp11::mp_size_t<2>, A&&... a ) { ::new( &t2_ ) T2( std::forward(a)... ); } template void emplace( mp11::mp_size_t<3>, A&&... a ) { ::new( &t3_ ) T3( std::forward(a)... ); } template void emplace( mp11::mp_size_t<4>, A&&... a ) { ::new( &t4_ ) T4( std::forward(a)... ); } template void emplace( mp11::mp_size_t<5>, A&&... a ) { ::new( &t5_ ) T5( std::forward(a)... ); } template void emplace( mp11::mp_size_t<6>, A&&... a ) { ::new( &t6_ ) T6( std::forward(a)... ); } template void emplace( mp11::mp_size_t<7>, A&&... a ) { ::new( &t7_ ) T7( std::forward(a)... ); } template void emplace( mp11::mp_size_t<8>, A&&... a ) { ::new( &t8_ ) T8( std::forward(a)... ); } template void emplace( mp11::mp_size_t<9>, A&&... a ) { ::new( &t9_ ) T9( std::forward(a)... ); } template void emplace( mp11::mp_size_t, A&&... a ) { rest_.emplace( mp11::mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T0& get( mp11::mp_size_t<0> ) noexcept { return t0_; } constexpr T0 const& get( mp11::mp_size_t<0> ) const noexcept { return t0_; } BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<1> ) noexcept { return t1_; } constexpr T1 const& get( mp11::mp_size_t<1> ) const noexcept { return t1_; } BOOST_CXX14_CONSTEXPR T2& get( mp11::mp_size_t<2> ) noexcept { return t2_; } constexpr T2 const& get( mp11::mp_size_t<2> ) const noexcept { return t2_; } BOOST_CXX14_CONSTEXPR T3& get( mp11::mp_size_t<3> ) noexcept { return t3_; } constexpr T3 const& get( mp11::mp_size_t<3> ) const noexcept { return t3_; } BOOST_CXX14_CONSTEXPR T4& get( mp11::mp_size_t<4> ) noexcept { return t4_; } constexpr T4 const& get( mp11::mp_size_t<4> ) const noexcept { return t4_; } BOOST_CXX14_CONSTEXPR T5& get( mp11::mp_size_t<5> ) noexcept { return t5_; } constexpr T5 const& get( mp11::mp_size_t<5> ) const noexcept { return t5_; } BOOST_CXX14_CONSTEXPR T6& get( mp11::mp_size_t<6> ) noexcept { return t6_; } constexpr T6 const& get( mp11::mp_size_t<6> ) const noexcept { return t6_; } BOOST_CXX14_CONSTEXPR T7& get( mp11::mp_size_t<7> ) noexcept { return t7_; } constexpr T7 const& get( mp11::mp_size_t<7> ) const noexcept { return t7_; } BOOST_CXX14_CONSTEXPR T8& get( mp11::mp_size_t<8> ) noexcept { return t8_; } constexpr T8 const& get( mp11::mp_size_t<8> ) const noexcept { return t8_; } BOOST_CXX14_CONSTEXPR T9& get( mp11::mp_size_t<9> ) noexcept { return t9_; } constexpr T9 const& get( mp11::mp_size_t<9> ) const noexcept { return t9_; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I-10>& get( mp11::mp_size_t ) noexcept { return rest_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I-10> const& get( mp11::mp_size_t ) const noexcept { return rest_.get( mp11::mp_size_t() ); } }; // all trivially destructible template union variant_storage_impl { T1 first_; variant_storage rest_; template constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): first_( std::forward(a)... ) { } template constexpr variant_storage_impl( mp11::mp_size_t, A&&... a ): rest_( mp11::mp_size_t(), std::forward(a)... ) { } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, mp11::mp_size_t, A&&... a ) { rest_.emplace( mp11::mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, mp11::mp_size_t, A&&... a ) { *this = variant_storage_impl( mp11::mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace( mp11::mp_size_t, A&&... a ) { this->emplace_impl( mp11::mp_all, detail::is_trivially_move_assignable...>(), mp11::mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp11::mp_size_t<0> ) const noexcept { return first_; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I-1>& get( mp11::mp_size_t ) noexcept { return rest_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I-1> const& get( mp11::mp_size_t ) const noexcept { return rest_.get( mp11::mp_size_t() ); } }; template union variant_storage_impl { T0 t0_; T1 t1_; T2 t2_; T3 t3_; T4 t4_; T5 t5_; T6 t6_; T7 t7_; T8 t8_; T9 t9_; variant_storage rest_; template constexpr variant_storage_impl( mp11::mp_size_t<0>, A&&... a ): t0_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<1>, A&&... a ): t1_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<2>, A&&... a ): t2_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<3>, A&&... a ): t3_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<4>, A&&... a ): t4_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<5>, A&&... a ): t5_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<6>, A&&... a ): t6_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<7>, A&&... a ): t7_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<8>, A&&... a ): t8_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t<9>, A&&... a ): t9_( std::forward(a)... ) {} template constexpr variant_storage_impl( mp11::mp_size_t, A&&... a ): rest_( mp11::mp_size_t(), std::forward(a)... ) {} template void emplace_impl( mp11::mp_false, mp11::mp_size_t<0>, A&&... a ) { ::new( &t0_ ) T0( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<1>, A&&... a ) { ::new( &t1_ ) T1( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<2>, A&&... a ) { ::new( &t2_ ) T2( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<3>, A&&... a ) { ::new( &t3_ ) T3( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<4>, A&&... a ) { ::new( &t4_ ) T4( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<5>, A&&... a ) { ::new( &t5_ ) T5( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<6>, A&&... a ) { ::new( &t6_ ) T6( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<7>, A&&... a ) { ::new( &t7_ ) T7( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<8>, A&&... a ) { ::new( &t8_ ) T8( std::forward(a)... ); } template void emplace_impl( mp11::mp_false, mp11::mp_size_t<9>, A&&... a ) { ::new( &t9_ ) T9( std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, mp11::mp_size_t, A&&... a ) { rest_.emplace( mp11::mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, mp11::mp_size_t, A&&... a ) { *this = variant_storage_impl( mp11::mp_size_t(), std::forward(a)... ); } template BOOST_CXX14_CONSTEXPR void emplace( mp11::mp_size_t, A&&... a ) { this->emplace_impl( mp11::mp_all< detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable, detail::is_trivially_move_assignable...>(), mp11::mp_size_t(), std::forward(a)... ); } BOOST_CXX14_CONSTEXPR T0& get( mp11::mp_size_t<0> ) noexcept { return t0_; } constexpr T0 const& get( mp11::mp_size_t<0> ) const noexcept { return t0_; } BOOST_CXX14_CONSTEXPR T1& get( mp11::mp_size_t<1> ) noexcept { return t1_; } constexpr T1 const& get( mp11::mp_size_t<1> ) const noexcept { return t1_; } BOOST_CXX14_CONSTEXPR T2& get( mp11::mp_size_t<2> ) noexcept { return t2_; } constexpr T2 const& get( mp11::mp_size_t<2> ) const noexcept { return t2_; } BOOST_CXX14_CONSTEXPR T3& get( mp11::mp_size_t<3> ) noexcept { return t3_; } constexpr T3 const& get( mp11::mp_size_t<3> ) const noexcept { return t3_; } BOOST_CXX14_CONSTEXPR T4& get( mp11::mp_size_t<4> ) noexcept { return t4_; } constexpr T4 const& get( mp11::mp_size_t<4> ) const noexcept { return t4_; } BOOST_CXX14_CONSTEXPR T5& get( mp11::mp_size_t<5> ) noexcept { return t5_; } constexpr T5 const& get( mp11::mp_size_t<5> ) const noexcept { return t5_; } BOOST_CXX14_CONSTEXPR T6& get( mp11::mp_size_t<6> ) noexcept { return t6_; } constexpr T6 const& get( mp11::mp_size_t<6> ) const noexcept { return t6_; } BOOST_CXX14_CONSTEXPR T7& get( mp11::mp_size_t<7> ) noexcept { return t7_; } constexpr T7 const& get( mp11::mp_size_t<7> ) const noexcept { return t7_; } BOOST_CXX14_CONSTEXPR T8& get( mp11::mp_size_t<8> ) noexcept { return t8_; } constexpr T8 const& get( mp11::mp_size_t<8> ) const noexcept { return t8_; } BOOST_CXX14_CONSTEXPR T9& get( mp11::mp_size_t<9> ) noexcept { return t9_; } constexpr T9 const& get( mp11::mp_size_t<9> ) const noexcept { return t9_; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I-10>& get( mp11::mp_size_t ) noexcept { return rest_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I-10> const& get( mp11::mp_size_t ) const noexcept { return rest_.get( mp11::mp_size_t() ); } }; // resolve_overload_* template struct overload; template<> struct overload<> { void operator()() const; }; template struct overload: overload { using overload::operator(); mp11::mp_identity operator()(T1) const; }; #if BOOST_WORKAROUND( BOOST_MSVC, < 1930 ) template using resolve_overload_type_ = decltype( overload()(std::declval()) ); template struct resolve_overload_type_impl: mp11::mp_defer< resolve_overload_type_, U, T... > { }; template using resolve_overload_type = typename resolve_overload_type_impl::type::type; #else template using resolve_overload_type = typename decltype( overload()(std::declval()) )::type; #endif template using resolve_overload_index = mp11::mp_find, resolve_overload_type>; // variant_base template struct variant_base_impl; template using variant_base = variant_base_impl...>::value, mp11::mp_all...>::value, T...>; struct none {}; // trivially destructible, single buffered template struct variant_base_impl { unsigned ix_; variant_storage st_; constexpr variant_base_impl(): ix_( 0 ), st_( mp11::mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st_( mp11::mp_size_t(), std::forward(a)... ) { } // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &st_ ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = I::value + 1; } constexpr std::size_t index() const noexcept { return ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { size_t const J = I+1; BOOST_ASSERT( ix_ == J ); return st_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { // size_t const J = I+1; BOOST_VARIANT2_CX14_ASSERT( ix_ == I+1 ) return st_.get( mp11::mp_size_t() ); } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_true, A&&... a ) { static_assert( std::is_nothrow_constructible::value, "Logic error: U must be nothrow constructible from A&&..." ); st_.emplace( mp11::mp_size_t(), std::forward(a)... ); ix_ = J; } template BOOST_CXX14_CONSTEXPR void emplace_impl( mp11::mp_false, A&&... a ) { static_assert( std::is_nothrow_move_constructible::value, "Logic error: U must be nothrow move constructible" ); U tmp( std::forward(a)... ); st_.emplace( mp11::mp_size_t(), std::move(tmp) ); ix_ = J; } template BOOST_CXX14_CONSTEXPR void emplace( A&&... a ) { std::size_t const J = I+1; using U = mp11::mp_at_c, I>; this->emplace_impl( std::is_nothrow_constructible(), std::forward(a)... ); } }; // trivially destructible, double buffered template struct variant_base_impl { unsigned ix_; variant_storage st_[ 2 ]; constexpr variant_base_impl(): ix_( 0 ), st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } } { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( ( I::value + 1 ) * 2 ), st_{ { mp11::mp_size_t(), std::forward(a)... }, { mp11::mp_size_t<0>() } } { } // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &st_[ 0 ] ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = ( I::value + 1 ) * 2; } constexpr std::size_t index() const noexcept { return ix_ / 2 - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { BOOST_ASSERT( index() == I ); size_t const J = I+1; constexpr mp11::mp_size_t j{}; return st_[ ix_ & 1 ].get( j ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { BOOST_VARIANT2_CX14_ASSERT( index() == I ) // size_t const J = I+1; // constexpr mp_size_t j{}; return st_[ ix_ & 1 ].get( mp11::mp_size_t() ); } template BOOST_CXX14_CONSTEXPR void emplace( A&&... a ) { size_t const J = I+1; unsigned i2 = 1 - ( ix_ & 1 ); st_[ i2 ].emplace( mp11::mp_size_t(), std::forward(a)... ); ix_ = J * 2 + i2; } }; // not trivially destructible, single buffered template struct variant_base_impl { unsigned ix_; variant_storage st_; constexpr variant_base_impl(): ix_( 0 ), st_( mp11::mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( I::value + 1 ), st_( mp11::mp_size_t(), std::forward(a)... ) { } // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &st_ ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = I::value + 1; } //[&]( auto I ){ // using U = mp_at_c, I>; // st1_.get( I ).~U(); //} struct _destroy_L1 { variant_base_impl * this_; template void operator()( I ) const noexcept { using U = mp11::mp_at, I>; this_->st_.get( I() ).~U(); } }; void _destroy() noexcept { if( ix_ > 0 ) { mp11::mp_with_index<1 + sizeof...(T)>( ix_, _destroy_L1{ this } ); } } ~variant_base_impl() noexcept { _destroy(); } constexpr std::size_t index() const noexcept { return ix_ - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { size_t const J = I+1; BOOST_ASSERT( ix_ == J ); return st_.get( mp11::mp_size_t() ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { // size_t const J = I+1; BOOST_VARIANT2_CX14_ASSERT( ix_ == I+1 ) return st_.get( mp11::mp_size_t() ); } template void emplace( A&&... a ) { size_t const J = I+1; using U = mp11::mp_at_c, I>; static_assert( std::is_nothrow_move_constructible::value, "Logic error: U must be nothrow move constructible" ); U tmp( std::forward(a)... ); _destroy(); st_.emplace( mp11::mp_size_t(), std::move(tmp) ); ix_ = J; } }; // not trivially destructible, double buffered template struct variant_base_impl { unsigned ix_; #if defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__) && !defined(__INTEL_COMPILER) // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63707 :-( variant_storage st1_, st2_; constexpr variant_base_impl(): ix_( 0 ), st1_( mp11::mp_size_t<0>() ), st2_( mp11::mp_size_t<0>() ) { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( ( I::value + 1 ) * 2 ), st1_( mp11::mp_size_t(), std::forward(a)... ), st2_( mp11::mp_size_t<0>() ) { } BOOST_CXX14_CONSTEXPR variant_storage& storage( unsigned i2 ) noexcept { return i2 == 0? st1_: st2_; } constexpr variant_storage const& storage( unsigned i2 ) const noexcept { return i2 == 0? st1_: st2_; } #else variant_storage st_[ 2 ]; constexpr variant_base_impl(): ix_( 0 ), st_{ { mp11::mp_size_t<0>() }, { mp11::mp_size_t<0>() } } { } template constexpr explicit variant_base_impl( I, A&&... a ): ix_( ( I::value + 1 ) * 2 ), st_{ { mp11::mp_size_t(), std::forward(a)... }, { mp11::mp_size_t<0>() } } { } BOOST_CXX14_CONSTEXPR variant_storage& storage( unsigned i2 ) noexcept { return st_[ i2 ]; } constexpr variant_storage const& storage( unsigned i2 ) const noexcept { return st_[ i2 ]; } #endif // requires: ix_ == 0 template void _replace( I, A&&... a ) { ::new( &storage( 0 ) ) variant_storage( mp11::mp_size_t(), std::forward(a)... ); ix_ = ( I::value + 1 ) * 2; } //[&]( auto I ){ // using U = mp_at_c, I>; // st1_.get( I ).~U(); //} struct _destroy_L1 { variant_base_impl * this_; unsigned i2_; template void operator()( I ) const noexcept { using U = mp11::mp_at, I>; this_->storage( i2_ ).get( I() ).~U(); } }; void _destroy() noexcept { mp11::mp_with_index<1 + sizeof...(T)>( ix_ / 2, _destroy_L1{ this, ix_ & 1 } ); } ~variant_base_impl() noexcept { _destroy(); } constexpr std::size_t index() const noexcept { return ix_ / 2 - 1; } template BOOST_CXX14_CONSTEXPR mp11::mp_at_c, I>& _get_impl( mp11::mp_size_t ) noexcept { BOOST_ASSERT( index() == I ); size_t const J = I+1; constexpr mp11::mp_size_t j{}; return storage( ix_ & 1 ).get( j ); } template constexpr mp11::mp_at_c, I> const& _get_impl( mp11::mp_size_t ) const noexcept { BOOST_VARIANT2_CX14_ASSERT( index() == I ) // size_t const J = I+1; // constexpr mp_size_t j{}; return storage( ix_ & 1 ).get( mp11::mp_size_t() ); } template void emplace( A&&... a ) { size_t const J = I+1; unsigned i2 = 1 - ( ix_ & 1 ); storage( i2 ).emplace( mp11::mp_size_t(), std::forward(a)... ); _destroy(); ix_ = J * 2 + i2; } }; } // namespace detail // in_place_type_t template struct in_place_type_t { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template constexpr in_place_type_t in_place_type{}; #endif namespace detail { template struct is_in_place_type: std::false_type {}; template struct is_in_place_type>: std::true_type {}; } // namespace detail // in_place_index_t template struct in_place_index_t { }; #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) template constexpr in_place_index_t in_place_index{}; #endif namespace detail { template struct is_in_place_index: std::false_type {}; template struct is_in_place_index>: std::true_type {}; } // namespace detail // is_nothrow_swappable namespace detail { namespace det2 { using std::swap; template using is_swappable_impl = decltype(swap(std::declval(), std::declval())); #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template struct is_nothrow_swappable_impl_ { static constexpr bool value = noexcept(swap(std::declval(), std::declval())); }; template using is_nothrow_swappable_impl = mp11::mp_bool< is_nothrow_swappable_impl_::value >; #else template using is_nothrow_swappable_impl = typename std::enable_if(), std::declval()))>::type; #endif } // namespace det2 template struct is_swappable: mp11::mp_valid { }; #if BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template struct is_nothrow_swappable: mp11::mp_eval_if>, mp11::mp_false, det2::is_nothrow_swappable_impl, T> { }; #else template struct is_nothrow_swappable: mp11::mp_valid { }; #endif // variant_cc_base template struct variant_cc_base_impl; template using variant_cc_base = variant_cc_base_impl< mp11::mp_all...>::value, mp11::mp_all...>::value, T...>; template struct variant_cc_base_impl: public variant_base { using variant_base = detail::variant_base; using variant_base::variant_base; variant_cc_base_impl() = default; variant_cc_base_impl( variant_cc_base_impl const& ) = default; variant_cc_base_impl( variant_cc_base_impl && ) = default; variant_cc_base_impl& operator=( variant_cc_base_impl const& ) = default; variant_cc_base_impl& operator=( variant_cc_base_impl && ) = default; }; template struct variant_cc_base_impl: public variant_base { using variant_base = detail::variant_base; using variant_base::variant_base; variant_cc_base_impl() = default; variant_cc_base_impl( variant_cc_base_impl const& ) = delete; variant_cc_base_impl( variant_cc_base_impl && ) = default; variant_cc_base_impl& operator=( variant_cc_base_impl const& ) = default; variant_cc_base_impl& operator=( variant_cc_base_impl && ) = default; }; template struct variant_cc_base_impl: public variant_base { using variant_base = detail::variant_base; using variant_base::variant_base; public: // constructors variant_cc_base_impl() = default; // copy constructor private: struct L1 { variant_base * this_; variant_base const & r; template void operator()( I i ) const { this_->_replace( i, r._get_impl( i ) ); } }; public: variant_cc_base_impl( variant_cc_base_impl const& r ) noexcept( mp11::mp_all...>::value ) : variant_base() { mp11::mp_with_index( r.index(), L1{ this, r } ); } // move constructor variant_cc_base_impl( variant_cc_base_impl && ) = default; // assignment variant_cc_base_impl& operator=( variant_cc_base_impl const & ) = default; variant_cc_base_impl& operator=( variant_cc_base_impl && ) = default; }; // variant_ca_base template struct variant_ca_base_impl; template using variant_ca_base = variant_ca_base_impl< mp11::mp_all..., std::is_copy_assignable...>::value, mp11::mp_all..., detail::is_trivially_copy_constructible..., detail::is_trivially_copy_assignable...>::value, T...>; template struct variant_ca_base_impl: public variant_cc_base { using variant_base = detail::variant_cc_base; using variant_base::variant_base; variant_ca_base_impl() = default; variant_ca_base_impl( variant_ca_base_impl const& ) = default; variant_ca_base_impl( variant_ca_base_impl && ) = default; variant_ca_base_impl& operator=( variant_ca_base_impl const& ) = default; variant_ca_base_impl& operator=( variant_ca_base_impl && ) = default; }; template struct variant_ca_base_impl: public variant_cc_base { using variant_base = detail::variant_cc_base; using variant_base::variant_base; variant_ca_base_impl() = default; variant_ca_base_impl( variant_ca_base_impl const& ) = default; variant_ca_base_impl( variant_ca_base_impl && ) = default; variant_ca_base_impl& operator=( variant_ca_base_impl const& ) = delete; variant_ca_base_impl& operator=( variant_ca_base_impl && ) = default; }; template struct variant_ca_base_impl: public variant_cc_base { using variant_base = detail::variant_cc_base; using variant_base::variant_base; public: // constructors variant_ca_base_impl() = default; variant_ca_base_impl( variant_ca_base_impl const& ) = default; variant_ca_base_impl( variant_ca_base_impl && ) = default; // copy assignment private: struct L3 { variant_base * this_; variant_base const & r; template void operator()( I i ) const { this_->template emplace( r._get_impl( i ) ); } }; public: BOOST_CXX14_CONSTEXPR variant_ca_base_impl& operator=( variant_ca_base_impl const & r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L3{ this, r } ); return *this; } // move assignment variant_ca_base_impl& operator=( variant_ca_base_impl && ) = default; }; // variant_mc_base template struct variant_mc_base_impl; template using variant_mc_base = variant_mc_base_impl< mp11::mp_all...>::value, mp11::mp_all...>::value, T...>; template struct variant_mc_base_impl: public variant_ca_base { using variant_base = detail::variant_ca_base; using variant_base::variant_base; variant_mc_base_impl() = default; variant_mc_base_impl( variant_mc_base_impl const& ) = default; variant_mc_base_impl( variant_mc_base_impl && ) = default; variant_mc_base_impl& operator=( variant_mc_base_impl const& ) = default; variant_mc_base_impl& operator=( variant_mc_base_impl && ) = default; }; template struct variant_mc_base_impl: public variant_ca_base { using variant_base = detail::variant_ca_base; using variant_base::variant_base; variant_mc_base_impl() = default; variant_mc_base_impl( variant_mc_base_impl const& ) = default; variant_mc_base_impl( variant_mc_base_impl && ) = delete; variant_mc_base_impl& operator=( variant_mc_base_impl const& ) = default; variant_mc_base_impl& operator=( variant_mc_base_impl && ) = default; }; template struct variant_mc_base_impl: public variant_ca_base { using variant_base = detail::variant_ca_base; using variant_base::variant_base; public: // constructors variant_mc_base_impl() = default; variant_mc_base_impl( variant_mc_base_impl const& ) = default; // move constructor private: struct L2 { variant_base * this_; variant_base & r; template void operator()( I i ) const { this_->_replace( i, std::move( r._get_impl( i ) ) ); } }; public: variant_mc_base_impl( variant_mc_base_impl && r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L2{ this, r } ); } // assignment variant_mc_base_impl& operator=( variant_mc_base_impl const & ) = default; variant_mc_base_impl& operator=( variant_mc_base_impl && ) = default; }; // variant_ma_base template struct variant_ma_base_impl; template using variant_ma_base = variant_ma_base_impl< mp11::mp_all..., std::is_move_assignable...>::value, mp11::mp_all..., detail::is_trivially_move_constructible..., detail::is_trivially_move_assignable...>::value, T...>; template struct variant_ma_base_impl: public variant_mc_base { using variant_base = detail::variant_mc_base; using variant_base::variant_base; variant_ma_base_impl() = default; variant_ma_base_impl( variant_ma_base_impl const& ) = default; variant_ma_base_impl( variant_ma_base_impl && ) = default; variant_ma_base_impl& operator=( variant_ma_base_impl const& ) = default; variant_ma_base_impl& operator=( variant_ma_base_impl && ) = default; }; template struct variant_ma_base_impl: public variant_mc_base { using variant_base = detail::variant_mc_base; using variant_base::variant_base; variant_ma_base_impl() = default; variant_ma_base_impl( variant_ma_base_impl const& ) = default; variant_ma_base_impl( variant_ma_base_impl && ) = default; variant_ma_base_impl& operator=( variant_ma_base_impl const& ) = default; variant_ma_base_impl& operator=( variant_ma_base_impl && ) = delete; }; template struct variant_ma_base_impl: public variant_mc_base { using variant_base = detail::variant_mc_base; using variant_base::variant_base; public: // constructors variant_ma_base_impl() = default; variant_ma_base_impl( variant_ma_base_impl const& ) = default; variant_ma_base_impl( variant_ma_base_impl && ) = default; // copy assignment variant_ma_base_impl& operator=( variant_ma_base_impl const & ) = default; // move assignment private: struct L4 { variant_base * this_; variant_base & r; template void operator()( I i ) const { this_->template emplace( std::move( r._get_impl( i ) ) ); } }; public: variant_ma_base_impl& operator=( variant_ma_base_impl && r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L4{ this, r } ); return *this; } }; } // namespace detail // variant template class variant: private detail::variant_ma_base { private: using variant_base = detail::variant_ma_base; public: // constructors template> >, E1>> constexpr variant() noexcept( std::is_nothrow_default_constructible< mp11::mp_first> >::value ) : variant_base( mp11::mp_size_t<0>() ) { } // variant( variant const& ) = default; // variant( variant && ) = default; template::type, class E1 = typename std::enable_if< !std::is_same::value && !std::is_base_of::value && !detail::is_in_place_index::value && !detail::is_in_place_type::value >::type, class V = detail::resolve_overload_type, class E2 = typename std::enable_if::value>::type > constexpr variant( U&& u ) noexcept( std::is_nothrow_constructible::value ) : variant_base( detail::resolve_overload_index(), std::forward(u) ) { } template, U>, class E = typename std::enable_if::value>::type> constexpr explicit variant( in_place_type_t, A&&... a ): variant_base( I(), std::forward(a)... ) { } template, U>, class E = typename std::enable_if&, A&&...>::value>::type> constexpr explicit variant( in_place_type_t, std::initializer_list il, A&&... a ): variant_base( I(), il, std::forward(a)... ) { } template, I>, A&&...>::value>::type> constexpr explicit variant( in_place_index_t, A&&... a ): variant_base( mp11::mp_size_t(), std::forward(a)... ) { } template, I>, std::initializer_list&, A&&...>::value>::type> constexpr explicit variant( in_place_index_t, std::initializer_list il, A&&... a ): variant_base( mp11::mp_size_t(), il, std::forward(a)... ) { } // assignment // variant& operator=( variant const& ) = default; // variant& operator=( variant && ) = default; template::type, variant>::value>::type, class V = detail::resolve_overload_type, class E2 = typename std::enable_if::value && std::is_constructible::value>::type > BOOST_CXX14_CONSTEXPR variant& operator=( U&& u ) noexcept( std::is_nothrow_constructible::value ) { std::size_t const I = detail::resolve_overload_index::value; this->template emplace( std::forward(u) ); return *this; } // modifiers template, U>::value == 1 && std::is_constructible::value >::type> BOOST_CXX14_CONSTEXPR U& emplace( A&&... a ) { using I = mp11::mp_find, U>; variant_base::template emplace( std::forward(a)... ); return _get_impl( I() ); } template, U>::value == 1 && std::is_constructible&, A&&...>::value >::type> BOOST_CXX14_CONSTEXPR U& emplace( std::initializer_list il, A&&... a ) { using I = mp11::mp_find, U>; variant_base::template emplace( il, std::forward(a)... ); return _get_impl( I() ); } template, I>, A&&...>::value>::type> BOOST_CXX14_CONSTEXPR variant_alternative_t>& emplace( A&&... a ) { variant_base::template emplace( std::forward(a)... ); return _get_impl( mp11::mp_size_t() ); } template, I>, std::initializer_list&, A&&...>::value>::type> BOOST_CXX14_CONSTEXPR variant_alternative_t>& emplace( std::initializer_list il, A&&... a ) { variant_base::template emplace( il, std::forward(a)... ); return _get_impl( mp11::mp_size_t() ); } // value status constexpr bool valueless_by_exception() const noexcept { return false; } using variant_base::index; // swap private: struct L5 { variant * this_; variant & r; template void operator()( I i ) const { using std::swap; swap( this_->_get_impl( i ), r._get_impl( i ) ); } }; public: void swap( variant& r ) noexcept( mp11::mp_all..., detail::is_nothrow_swappable...>::value ) { if( index() == r.index() ) { mp11::mp_with_index( index(), L5{ this, r } ); } else { variant tmp( std::move(*this) ); *this = std::move( r ); r = std::move( tmp ); } } // private accessors using variant_base::_get_impl; // converting constructors (extension) private: template struct L6 { variant_base * this_; variant const & r; template void operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; this_->_replace( J{}, r._get_impl( i ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > variant( variant const& r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L6{ this, r } ); } private: template struct L7 { variant_base * this_; variant & r; template void operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; this_->_replace( J{}, std::move( r._get_impl( i ) ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > variant( variant && r ) noexcept( mp11::mp_all...>::value ) { mp11::mp_with_index( r.index(), L7{ this, r } ); } // subset (extension) private: template::type> static constexpr variant _subset_impl( mp11::mp_size_t, V && v ) { return variant( in_place_index_t(), std::forward(v) ); } template static variant _subset_impl( mp11::mp_size_t, V && /*v*/ ) { detail::throw_bad_variant_access(); } private: template struct L8 { variant * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, this_->_get_impl( i ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > BOOST_CXX14_CONSTEXPR variant subset() & { return mp11::mp_with_index( index(), L8{ this } ); } private: template struct L9 { variant const * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, this_->_get_impl( i ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > constexpr variant subset() const& { return mp11::mp_with_index( index(), L9{ this } ); } private: template struct L10 { variant * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, std::move( this_->_get_impl( i ) ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > BOOST_CXX14_CONSTEXPR variant subset() && { return mp11::mp_with_index( index(), L10{ this } ); } #if !BOOST_WORKAROUND(BOOST_GCC, < 40900) // g++ 4.8 doesn't handle const&& particularly well private: template struct L11 { variant const * this_; template variant operator()( I i ) const { using J = mp11::mp_find, mp11::mp_at, I>>; return this_->_subset_impl( J{}, std::move( this_->_get_impl( i ) ) ); } }; public: template..., mp11::mp_contains, U>...>, void> > constexpr variant subset() const&& { return mp11::mp_with_index( index(), L11{ this } ); } #endif }; // relational operators namespace detail { template struct eq_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) == w._get_impl( i ); } }; } // namespace detail template constexpr bool operator==( variant const & v, variant const & w ) { return v.index() == w.index() && mp11::mp_with_index( v.index(), detail::eq_L{ v, w } ); } namespace detail { template struct ne_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) != w._get_impl( i ); } }; } // namespace detail template constexpr bool operator!=( variant const & v, variant const & w ) { return v.index() != w.index() || mp11::mp_with_index( v.index(), detail::ne_L{ v, w } ); } namespace detail { template struct lt_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) < w._get_impl( i ); } }; } // namespace detail template constexpr bool operator<( variant const & v, variant const & w ) { return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index( v.index(), detail::lt_L{ v, w } ) ); } template constexpr bool operator>( variant const & v, variant const & w ) { return w < v; } namespace detail { template struct le_L { variant const & v; variant const & w; template constexpr bool operator()( I i ) const { return v._get_impl( i ) <= w._get_impl( i ); } }; } // namespace detail template constexpr bool operator<=( variant const & v, variant const & w ) { return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index( v.index(), detail::le_L{ v, w } ) ); } template constexpr bool operator>=( variant const & v, variant const & w ) { return w <= v; } // visitation namespace detail { template using remove_cv_ref_t = typename std::remove_cv::type>::type; template variant const & extract_variant_base_( variant const & ); #if BOOST_WORKAROUND( BOOST_MSVC, < 1930 ) template struct extract_vbase_impl { using type = decltype( extract_variant_base_( std::declval() ) ); }; template using extract_variant_base = remove_cv_ref_t< typename extract_vbase_impl::type >; #else template using extract_variant_base = remove_cv_ref_t< decltype( extract_variant_base_( std::declval() ) ) >; #endif template using variant_base_size = variant_size< extract_variant_base >; template struct copy_cv_ref { using type = T; }; template struct copy_cv_ref { using type = T const; }; template struct copy_cv_ref { using type = T volatile; }; template struct copy_cv_ref { using type = T const volatile; }; template struct copy_cv_ref { using type = typename copy_cv_ref::type&; }; template struct copy_cv_ref { using type = typename copy_cv_ref::type&&; }; template using copy_cv_ref_t = typename copy_cv_ref::type; template struct Qret { template using fn = decltype( std::declval()( std::declval()... ) ); }; template using front_if_same = mp11::mp_if, mp11::mp_front>; template using apply_cv_ref = mp11::mp_product, mp11::mp_list>; struct deduced {}; #if !BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) template using Vret = mp11::mp_eval_if_not< std::is_same, R, front_if_same, mp11::mp_product_q, apply_cv_ref...> >; #else template struct Vret_impl { using type = mp11::mp_eval_if_not< std::is_same, R, front_if_same, mp11::mp_product_q, apply_cv_ref...> >; }; template using Vret = typename Vret_impl::type; #endif } // namespace detail template constexpr auto visit( F&& f ) -> detail::Vret { return std::forward(f)(); } namespace detail { template struct visit_L1 { F&& f; V1&& v1; template auto operator()( I ) const -> Vret { return std::forward(f)( unsafe_get( std::forward(v1) ) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L1{ std::forward(f), std::forward(v1) } ); } #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) || BOOST_WORKAROUND( BOOST_MSVC, < 1920 ) namespace detail { template struct bind_front_ { F&& f; A&& a; template auto operator()( T&&... t ) -> decltype( std::forward(f)( std::forward(a), std::forward(t)... ) ) { return std::forward(f)( std::forward(a), std::forward(t)... ); } }; template bind_front_ bind_front( F&& f, A&& a ) { return bind_front_{ std::forward(f), std::forward(a) }; } template struct visit_L2 { F&& f; V1&& v1; V2&& v2; template auto operator()( I ) const -> Vret { auto f2 = bind_front( std::forward(f), unsafe_get( std::forward(v1) ) ); return visit( f2, std::forward(v2) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1, V2&& v2 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L2{ std::forward(f), std::forward(v1), std::forward(v2) } ); } namespace detail { template struct visit_L3 { F&& f; V1&& v1; V2&& v2; V3&& v3; template auto operator()( I ) const -> Vret { auto f2 = bind_front( std::forward(f), unsafe_get( std::forward(v1) ) ); return visit( f2, std::forward(v2), std::forward(v3) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L3{ std::forward(f), std::forward(v1), std::forward(v2), std::forward(v3) } ); } namespace detail { template struct visit_L4 { F&& f; V1&& v1; V2&& v2; V3&& v3; V4&& v4; template auto operator()( I ) const -> Vret { auto f2 = bind_front( std::forward(f), unsafe_get( std::forward(v1) ) ); return visit( f2, std::forward(v2), std::forward(v3), std::forward(v4) ); } }; } // namespace detail template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3, V4&& v4 ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), detail::visit_L4{ std::forward(f), std::forward(v1), std::forward(v2), std::forward(v3), std::forward(v4) } ); } #else template constexpr auto visit( F&& f, V1&& v1, V2&& v2, V&&... v ) -> detail::Vret { return mp11::mp_with_index>( v1.index(), [&]( auto I ){ auto f2 = [&]( auto&&... a ){ return std::forward(f)( unsafe_get( std::forward(v1) ), std::forward(a)... ); }; return visit( f2, std::forward(v2), std::forward(v)... ); }); } #endif // specialized algorithms template..., detail::is_swappable...>::value>::type> void swap( variant & v, variant & w ) noexcept( noexcept(v.swap(w)) ) { v.swap( w ); } // visit_by_index namespace detail { template using Vret2 = mp11::mp_eval_if_not< std::is_same, R, front_if_same, mp11::mp_transform...>, apply_cv_ref> >; template struct visit_by_index_L { V&& v; std::tuple tp; template constexpr detail::Vret2 operator()( I ) const { return std::get( std::move(tp) )( unsafe_get( std::forward(v) ) ); } }; } // namespace detail template constexpr auto visit_by_index( V&& v, F&&... f ) -> detail::Vret2 { static_assert( variant_size::value == sizeof...(F), "Incorrect number of function objects" ); return mp11::mp_with_index::value>( v.index(), detail::visit_by_index_L{ std::forward(v), std::tuple( std::forward(f)... ) } ); } // output streaming namespace detail { template struct ostream_insert_L { std::basic_ostream& os; variant const& v; template std::basic_ostream& operator()( I ) const { return os << unsafe_get( v ); } }; } // namespace detail template std::basic_ostream& operator<<( std::basic_ostream& os, variant const& v ) { return mp11::mp_with_index<1 + sizeof...(T)>( v.index(), detail::ostream_insert_L{ os, v } ); } // hashing support namespace detail { inline std::size_t hash_value_impl_( mp11::mp_true, std::size_t index, std::size_t value ) { boost::ulong_long_type hv = ( boost::ulong_long_type( 0xCBF29CE4 ) << 32 ) + 0x84222325; boost::ulong_long_type const prime = ( boost::ulong_long_type( 0x00000100 ) << 32 ) + 0x000001B3; hv ^= index; hv *= prime; hv ^= value; hv *= prime; return static_cast( hv ); } inline std::size_t hash_value_impl_( mp11::mp_false, std::size_t index, std::size_t value ) { std::size_t hv = 0x811C9DC5; std::size_t const prime = 0x01000193; hv ^= index; hv *= prime; hv ^= value; hv *= prime; return hv; } inline std::size_t hash_value_impl( std::size_t index, std::size_t value ) { return hash_value_impl_( mp11::mp_bool< (SIZE_MAX > UINT32_MAX) >(), index, value ); } template class H, class V> struct hash_value_L { V const & v; template std::size_t operator()( I ) const { auto const & t = unsafe_get( v ); std::size_t index = I::value; std::size_t value = H>()( t ); return hash_value_impl( index, value ); } }; template std::size_t hash_value_std( variant const & v ) { return mp11::mp_with_index( v.index(), detail::hash_value_L< std::hash, variant >{ v } ); } } // namespace detail inline std::size_t hash_value( monostate const & ) { return 0xA7EE4757u; } template std::size_t hash_value( variant const & v ) { return mp11::mp_with_index( v.index(), detail::hash_value_L< boost::hash, variant >{ v } ); } namespace detail { template using is_hash_enabled = std::is_default_constructible< std::hash::type> >; template::value> struct std_hash_impl; template struct std_hash_impl { std_hash_impl() = delete; std_hash_impl( std_hash_impl const& ) = delete; std_hash_impl& operator=( std_hash_impl const& ) = delete; }; template struct std_hash_impl { std::size_t operator()( V const & v ) const { return detail::hash_value_std( v ); } }; } // namespace detail } // namespace variant2 } // namespace boost namespace std { template struct hash< ::boost::variant2::variant >: public ::boost::variant2::detail::std_hash_impl< ::boost::variant2::variant > { }; template<> struct hash< ::boost::variant2::monostate > { std::size_t operator()( ::boost::variant2::monostate const & v ) const { return hash_value( v ); } }; } // namespace std #undef BOOST_VARIANT2_CX14_ASSERT #if defined(_MSC_VER) && _MSC_VER < 1910 # pragma warning( pop ) #endif #endif // #ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED