Relax get<T> constraint to permit duplicate alternatives. Refs #33.

This commit is contained in:
Peter Dimov
2026-01-21 19:52:21 +02:00
parent 3b731db14d
commit 6e8f4e955d
10 changed files with 181 additions and 118 deletions

View File

@@ -1,12 +1,9 @@
#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
#define BOOST_VARIANT2_VARIANT_HPP_INCLUDED
// Copyright 2017-2019 Peter Dimov.
//
// Copyright 2017-2026 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
// https://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER) && _MSC_VER < 1910
# pragma warning( push )
@@ -451,61 +448,106 @@ template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T
// get (type)
namespace detail
{
template<class U, class V> struct get_if_impl_L1
{
V& v_;
template<class I> constexpr U* fn( I, mp11::mp_true ) const noexcept
{
return &v_._get_impl( I() );
}
template<class I> constexpr U* fn( I, mp11::mp_false ) const noexcept
{
return nullptr;
}
template<class I> constexpr U* operator()( I ) const noexcept
{
return this->fn( I(), std::is_same<U, mp11::mp_at<V, I>>() );
}
};
template<class U, class... T> constexpr U* get_if_impl( variant<T...>& v ) noexcept
{
return mp11::mp_with_index<sizeof...(T)>( v.index(), get_if_impl_L1< U, variant<T...> >{ v } );
}
template<class U, class V> struct get_if_impl_L2
{
V const& v_;
template<class I> constexpr U const* fn( I, mp11::mp_true ) const noexcept
{
return &v_._get_impl( I() );
}
template<class I> constexpr U const* fn( I, mp11::mp_false ) const noexcept
{
return nullptr;
}
template<class I> constexpr U const* operator()( I ) const noexcept
{
return this->fn( I(), std::is_same<U, mp11::mp_at<V, I>>() );
}
};
template<class U, class... T> constexpr U const* get_if_impl( variant<T...> const& v ) noexcept
{
return mp11::mp_with_index<sizeof...(T)>( v.index(), get_if_impl_L2< U, variant<T...> >{ v } );
}
} // namespace detail
template<class U, class... T> constexpr U& get(variant<T...>& v)
{
static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
using I = mp11::mp_find<variant<T...>, U>;
return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() );
static_assert( mp11::mp_contains<variant<T...>, U>::value, "The type must be present in the list of variant alternatives" );
return ( !holds_alternative<U>( v )? detail::throw_bad_variant_access(): (void)0 ), *detail::get_if_impl<U>( v );
}
template<class U, class... T> constexpr U&& get(variant<T...>&& v)
{
static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
using I = mp11::mp_find<variant<T...>, U>;
static_assert( mp11::mp_contains<variant<T...>, U>::value, "The type must be present in the list of variant alternatives" );
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) );
return ( !holds_alternative<U>( v )? detail::throw_bad_variant_access(): (void)0 ), std::move( *detail::get_if_impl<U>( v ) );
#else
if( v.index() != I::value ) detail::throw_bad_variant_access();
return std::move( v._get_impl( I() ) );
if( !holds_alternative<U>( v ) ) detail::throw_bad_variant_access();
return std::move( *detail::get_if_impl<U>( v ) );
#endif
}
template<class U, class... T> constexpr U const& get(variant<T...> const& v)
{
static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
using I = mp11::mp_find<variant<T...>, U>;
return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() );
static_assert( mp11::mp_contains<variant<T...>, U>::value, "The type must be present in the list of variant alternatives" );
return ( !holds_alternative<U>( v )? detail::throw_bad_variant_access(): (void)0 ), *detail::get_if_impl<U>( v );
}
template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
{
static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
using I = mp11::mp_find<variant<T...>, U>;
static_assert( mp11::mp_contains<variant<T...>, U>::value, "The type must be present in the list of variant alternatives" );
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1930)
return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) );
return ( !holds_alternative<U>( v )? detail::throw_bad_variant_access(): (void)0 ), std::move( *detail::get_if_impl<U>( v ) );
#else
if( v.index() != I::value ) detail::throw_bad_variant_access();
return std::move( v._get_impl( I() ) );
if( !holds_alternative<U>( v ) ) detail::throw_bad_variant_access();
return std::move( *detail::get_if_impl<U>( v ) );
#endif
}
// get_if
// get_if (index)
template<std::size_t I, class... T> constexpr typename std::add_pointer<variant_alternative_t<I, variant<T...>>>::type get_if(variant<T...>* v) noexcept
{
@@ -519,22 +561,18 @@ template<std::size_t I, class... T> constexpr typename std::add_pointer<const va
return v && v->index() == I? &v->_get_impl( mp11::mp_size_t<I>() ): 0;
}
// get_if (type)
template<class U, class... T> constexpr typename std::add_pointer<U>::type get_if(variant<T...>* v) noexcept
{
static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
using I = mp11::mp_find<variant<T...>, U>;
return v && v->index() == I::value? &v->_get_impl( I() ): 0;
static_assert( mp11::mp_contains<variant<T...>, U>::value, "The type must be present in the list of variant alternatives" );
return v && holds_alternative<U>( *v )? detail::get_if_impl<U>( *v ): 0;
}
template<class U, class... T> constexpr typename std::add_pointer<U const>::type get_if(variant<T...> const * v) noexcept
template<class U, class... T> constexpr typename std::add_pointer<U const>::type get_if(variant<T...> const* v) noexcept
{
static_assert( mp11::mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
using I = mp11::mp_find<variant<T...>, U>;
return v && v->index() == I::value? &v->_get_impl( I() ): 0;
static_assert( mp11::mp_contains<variant<T...>, U>::value, "The type must be present in the list of variant alternatives" );
return v && holds_alternative<U>( *v )? detail::get_if_impl<U>( *v ): 0;
}
//

View File

@@ -111,7 +111,12 @@ run variant_special.cpp ;
run variant_visit_derived.cpp ;
run variant_many_types.cpp ;
# These two tests require /bigobj under GCC/Windows and CMake/VS2022 (for some reason)
run variant_many_types.cpp : : :
<toolset>gcc,<target-os>windows:<variant>release
<toolset>gcc,<target-os>cygwin:<variant>release
;
run variant_visit_r.cpp : : :
<toolset>gcc,<target-os>windows:<variant>release

View File

@@ -43,26 +43,26 @@ enum E
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class V, class T, class A> constexpr T test( A const& a )
template<class V, std::size_t I, class A> constexpr variant_alternative_t<I, V> test( A const& a )
{
V v;
v = a;
return get<T>(v);
return get<I>(v);
}
int main()
{
{
constexpr variant<int> v( 1 );
constexpr auto w = test<variant<int>, int>( v );
constexpr auto w = test<variant<int>, 0>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<X> v( 1 );
constexpr auto w = test<variant<X>, X>( v );
constexpr auto w = test<variant<X>, 0>( v );
STATIC_ASSERT( w == 1 );
}
@@ -71,7 +71,7 @@ int main()
{
constexpr variant<Y> v( 1 );
constexpr auto w = test<variant<Y>, Y>( v );
constexpr auto w = test<variant<Y>, 0>( v );
STATIC_ASSERT( w == 1 );
}
@@ -79,31 +79,31 @@ int main()
{
constexpr variant<int, float> v( 1 );
constexpr auto w = test<variant<int, float>, int>( v );
constexpr auto w = test<variant<int, float>, 0>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, float> v( 3.0f );
constexpr auto w = test<variant<int, float>, float>( v );
constexpr auto w = test<variant<int, float>, 1>( v );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr variant<int, int, float> v( 3.0f );
constexpr auto w = test<variant<int, int, float>, float>( v );
constexpr auto w = test<variant<int, int, float>, 2>( v );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr variant<E, E, X> v( 1 );
constexpr auto w = test<variant<E, E, X>, X>( v );
constexpr auto w = test<variant<E, E, X>, 2>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, int, float, float, X> v( X(1) );
constexpr auto w = test<variant<int, int, float, float, X>, X>( v );
constexpr auto w = test<variant<int, int, float, float, X>, 4>( v );
STATIC_ASSERT( w == 1 );
}
@@ -112,13 +112,13 @@ int main()
{
constexpr variant<E, E, Y> v( 1 );
constexpr auto w = test<variant<E, E, Y>, Y>( v );
constexpr auto w = test<variant<E, E, Y>, 2>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, int, float, float, Y> v( Y(1) );
constexpr auto w = test<variant<int, int, float, float, Y>, Y>( v );
constexpr auto w = test<variant<int, int, float, float, Y>, 4>( v );
STATIC_ASSERT( w == 1 );
}

View File

@@ -38,22 +38,22 @@ enum E
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class T, class V> constexpr T test( V const v )
template<std::size_t I, class V> constexpr variant_alternative_t<I, V> test( V const v )
{
return get<T>( v );
return get<I>( v );
}
int main()
{
{
constexpr variant<int> v( 1 );
constexpr auto w = test<int>( v );
constexpr auto w = test<0>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<X> v( 1 );
constexpr auto w = test<X>( v );
constexpr auto w = test<0>( v );
STATIC_ASSERT( w == 1 );
}
@@ -62,7 +62,7 @@ int main()
{
constexpr variant<Y> v( 1 );
constexpr auto w = test<Y>( v );
constexpr auto w = test<0>( v );
STATIC_ASSERT( w == 1 );
}
@@ -70,31 +70,31 @@ int main()
{
constexpr variant<int, float> v( 1 );
constexpr auto w = test<int>( v );
constexpr auto w = test<0>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, float> v( 3.0f );
constexpr auto w = test<float>( v );
constexpr auto w = test<1>( v );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr variant<int, int, float> v( 3.0f );
constexpr auto w = test<float>( v );
constexpr auto w = test<2>( v );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr variant<E, E, X> v( 1 );
constexpr auto w = test<X>( v );
constexpr auto w = test<2>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, int, float, float, X> v( X(1) );
constexpr auto w = test<X>( v );
constexpr auto w = test<4>( v );
STATIC_ASSERT( w == 1 );
}
@@ -103,13 +103,13 @@ int main()
{
constexpr variant<E, E, Y> v( 1 );
constexpr auto w = test<Y>( v );
constexpr auto w = test<2>( v );
STATIC_ASSERT( w == 1 );
}
{
constexpr variant<int, int, float, float, Y> v( Y(1) );
constexpr auto w = test<Y>( v );
constexpr auto w = test<4>( v );
STATIC_ASSERT( w == 1 );
}

View File

@@ -33,24 +33,24 @@ struct Y
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class V, class T, class A> constexpr A test( A const& a )
template<class V, std::size_t I, class A, class T = variant_alternative_t<I, V>> constexpr T test( A const& a )
{
V v;
v.template emplace<T>( a );
return get<T>(v);
return get<I>(v);
}
int main()
{
{
constexpr auto w = test<variant<int>, int>( 1 );
constexpr auto w = test<variant<int>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<X>, X>( 1 );
constexpr auto w = test<variant<X>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
@@ -58,29 +58,29 @@ int main()
#else
{
constexpr auto w = test<variant<Y>, Y>( 1 );
constexpr auto w = test<variant<Y>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
#endif
{
constexpr auto w = test<variant<int, float>, int>( 1 );
constexpr auto w = test<variant<int, float>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<int, float>, float>( 3.0f );
constexpr auto w = test<variant<int, float>, 1>( 3.0f );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<variant<int, int, float>, float>( 3.0f );
constexpr auto w = test<variant<int, int, float>, 2>( 3.0f );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<variant<int, int, float, float, X>, X>( 1 );
constexpr auto w = test<variant<int, int, float, float, X>, 4>( 1 );
STATIC_ASSERT( w == 1 );
}
@@ -88,7 +88,7 @@ int main()
#else
{
constexpr auto w = test<variant<int, int, float, float, Y>, Y>( 1 );
constexpr auto w = test<variant<int, int, float, float, Y>, 4>( 1 );
STATIC_ASSERT( w == 1 );
}

View File

@@ -1,10 +1,6 @@
// Copyright 2017 Peter Dimov.
//
// Copyright 2017, 2026 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
// https://www.boost.org/LICENSE_1_0.txt
#if defined( __clang__ ) && defined( __has_warning )
# if __has_warning( "-Wdeprecated-volatile" )
@@ -217,6 +213,9 @@ int main()
BOOST_TEST_EQ( get<int>(v), 0 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<float>(&v), nullptr );
BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
}
@@ -226,6 +225,9 @@ int main()
BOOST_TEST_EQ( get<int>(v), 0 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<float>(&v), nullptr );
BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
}
@@ -235,6 +237,9 @@ int main()
BOOST_TEST_EQ( get<int>(v), 1 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<float>(&v), nullptr );
BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
}
@@ -244,12 +249,18 @@ int main()
BOOST_TEST_EQ( get<int>(v), 1 );
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<float>(&v), nullptr );
BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
}
{
variant<int, int, float> v( 3.14f );
BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<int>(&v), nullptr );
BOOST_TEST_EQ( get<float>(v), 3.14f );
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
@@ -259,6 +270,9 @@ int main()
{
variant<int, int, float> const v( 3.14f );
BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<int>(&v), nullptr );
BOOST_TEST_EQ( get<float>(v), 3.14f );
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
@@ -301,11 +315,13 @@ int main()
{
variant<int, int, float> * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
{
variant<int, int, float> const * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}

View File

@@ -3,12 +3,13 @@
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/mp11.hpp>
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
#if !defined(BOOST_MP11_HAS_CXX14_CONSTEXPR)
BOOST_PRAGMA_MESSAGE( "Test skipped because BOOST_MSVC < 1910" )
BOOST_PRAGMA_MESSAGE("Test skipped because BOOST_MP11_HAS_CXX14_CONSTEXPR is not defined")
int main() {}
#else
@@ -74,6 +75,7 @@ int main()
STATIC_ASSERT( get<int>(v) == 0 );
STATIC_ASSERT_IF( get_if<int>(&v) == &get<int>(v) );
STATIC_ASSERT_IF( get_if<float>(&v) == nullptr );
}
{
@@ -82,6 +84,7 @@ int main()
STATIC_ASSERT( get<int>(v) == 1 );
STATIC_ASSERT_IF( get_if<int>(&v) == &get<int>(v) );
STATIC_ASSERT_IF( get_if<float>(&v) == nullptr );
}
{
@@ -89,6 +92,7 @@ int main()
STATIC_ASSERT( get<float>(v) == (float)3.14f );
STATIC_ASSERT_IF( get_if<int>(&v) == nullptr );
STATIC_ASSERT_IF( get_if<float>(&v) == &get<float>(v) );
}
}

View File

@@ -44,24 +44,24 @@ enum E
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class V, class T, class A> constexpr T test( A&& a )
template<class V, std::size_t I, class A> constexpr variant_alternative_t<I, V> test( A&& a )
{
V v;
v = std::forward<A>(a);
return get<T>(v);
return get<I>(v);
}
int main()
{
{
constexpr auto w = test<variant<int>, int>( variant<int>( 1 ) );
constexpr auto w = test<variant<int>, 0>( variant<int>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<X>, X>( variant<X>( 1 ) );
constexpr auto w = test<variant<X>, 0>( variant<X>( 1 ) );
STATIC_ASSERT( w == 1 );
}
@@ -69,34 +69,34 @@ int main()
#else
{
constexpr auto w = test<variant<Y>, Y>( variant<Y>( 1 ) );
constexpr auto w = test<variant<Y>, 0>( variant<Y>( 1 ) );
STATIC_ASSERT( w == 1 );
}
#endif
{
constexpr auto w = test<variant<int, float>, int>( variant<int, float>( 1 ) );
constexpr auto w = test<variant<int, float>, 0>( variant<int, float>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<int, float>, float>( variant<int, float>( 3.0f ) );
constexpr auto w = test<variant<int, float>, 1>( variant<int, float>( 3.0f ) );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<variant<int, int, float>, float>( variant<int, int, float>( 3.0f ) );
constexpr auto w = test<variant<int, int, float>, 2>( variant<int, int, float>( 3.0f ) );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<variant<E, E, X>, X>( variant<E, E, X>( 1 ) );
constexpr auto w = test<variant<E, E, X>, 2>( variant<E, E, X>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<int, int, float, float, X>, X>( variant<int, int, float, float, X>( X(1) ) );
constexpr auto w = test<variant<int, int, float, float, X>, 4>( variant<int, int, float, float, X>( X(1) ) );
STATIC_ASSERT( w == 1 );
}
@@ -104,12 +104,12 @@ int main()
#else
{
constexpr auto w = test<variant<E, E, Y>, Y>( variant<E, E, Y>( 1 ) );
constexpr auto w = test<variant<E, E, Y>, 2>( variant<E, E, Y>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<int, int, float, float, Y>, Y>( variant<int, int, float, float, Y>( Y(1) ) );
constexpr auto w = test<variant<int, int, float, float, Y>, 4>( variant<int, int, float, float, Y>( Y(1) ) );
STATIC_ASSERT( w == 1 );
}

View File

@@ -39,21 +39,21 @@ enum E
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class T, class V> constexpr T test( V&& v )
template<std::size_t I, class V> constexpr variant_alternative_t<I, V> test( V&& v )
{
V v2( std::forward<V>(v) );
return get<T>( v2 );
return get<I>( v2 );
}
int main()
{
{
constexpr auto w = test<int>( variant<int>( 1 ) );
constexpr auto w = test<0>( variant<int>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<X>( variant<X>( 1 ) );
constexpr auto w = test<0>( variant<X>( 1 ) );
STATIC_ASSERT( w == 1 );
}
@@ -61,34 +61,34 @@ int main()
#else
{
constexpr auto w = test<Y>( variant<Y>( 1 ) );
constexpr auto w = test<0>( variant<Y>( 1 ) );
STATIC_ASSERT( w == 1 );
}
#endif
{
constexpr auto w = test<int>( variant<int, float>( 1 ) );
constexpr auto w = test<0>( variant<int, float>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<float>( variant<int, float>( 3.0f ) );
constexpr auto w = test<1>( variant<int, float>( 3.0f ) );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<float>( variant<int, int, float>( 3.0f ) );
constexpr auto w = test<2>( variant<int, int, float>( 3.0f ) );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<X>( variant<E, E, X>( 1 ) );
constexpr auto w = test<2>( variant<E, E, X>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<X>( variant<int, int, float, float, X>( X(1) ) );
constexpr auto w = test<4>( variant<int, int, float, float, X>( X(1) ) );
STATIC_ASSERT( w == 1 );
}
@@ -96,12 +96,12 @@ int main()
#else
{
constexpr auto w = test<Y>( variant<E, E, Y>( 1 ) );
constexpr auto w = test<2>( variant<E, E, Y>( 1 ) );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<Y>( variant<int, int, float, float, Y>( Y(1) ) );
constexpr auto w = test<4>( variant<int, int, float, float, Y>( Y(1) ) );
STATIC_ASSERT( w == 1 );
}

View File

@@ -38,24 +38,24 @@ enum E
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template<class V, class T, class A> constexpr A test( A const& a )
template<class V, std::size_t I, class A> constexpr variant_alternative_t<I, V> test( A const& a )
{
V v;
v = a;
return get<T>(v);
return get<I>(v);
}
int main()
{
{
constexpr auto w = test<variant<int>, int>( 1 );
constexpr auto w = test<variant<int>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<X>, X>( 1 );
constexpr auto w = test<variant<X>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
@@ -63,34 +63,34 @@ int main()
#else
{
constexpr auto w = test<variant<Y>, Y>( 1 );
constexpr auto w = test<variant<Y>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
#endif
{
constexpr auto w = test<variant<int, float>, int>( 1 );
constexpr auto w = test<variant<int, float>, 0>( 1 );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<int, float>, float>( 3.0f );
constexpr auto w = test<variant<int, float>, 1>( 3.0f );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<variant<int, int, float>, float>( 3.0f );
constexpr auto w = test<variant<int, int, float>, 2>( 3.0f );
STATIC_ASSERT( w == 3.0f );
}
{
constexpr auto w = test<variant<E, E, X>, X>( 1 );
constexpr auto w = test<variant<E, E, X>, 2>( 1 );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<int, int, float, float, X>, X>( X(1) );
constexpr auto w = test<variant<int, int, float, float, X>, 4>( X(1) );
STATIC_ASSERT( w == 1 );
}
@@ -98,12 +98,12 @@ int main()
#else
{
constexpr auto w = test<variant<E, E, Y>, Y>( 1 );
constexpr auto w = test<variant<E, E, Y>, 2>( 1 );
STATIC_ASSERT( w == 1 );
}
{
constexpr auto w = test<variant<int, int, float, float, Y>, Y>( Y(1) );
constexpr auto w = test<variant<int, int, float, float, Y>, 4>( Y(1) );
STATIC_ASSERT( w == 1 );
}