2026-01-21 14:58:47 +02:00
|
|
|
// Copyright 2017, 2026 Peter Dimov.
|
2017-06-09 17:24:23 +03:00
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
2026-01-21 14:58:47 +02:00
|
|
|
// https://www.boost.org/LICENSE_1_0.txt
|
2017-06-09 17:24:23 +03:00
|
|
|
|
|
|
|
|
#include <boost/variant2/variant.hpp>
|
2026-01-21 14:58:47 +02:00
|
|
|
#include <boost/config.hpp>
|
|
|
|
|
#include <boost/config/pragma_message.hpp>
|
2017-06-09 17:24:23 +03:00
|
|
|
#include <utility>
|
|
|
|
|
|
2026-01-21 14:58:47 +02:00
|
|
|
#if defined(BOOST_NO_CXX14_CONSTEXPR)
|
|
|
|
|
|
|
|
|
|
BOOST_PRAGMA_MESSAGE( "Test skipped because BOOST_NO_CXX14_CONSTEXPR is defined" )
|
|
|
|
|
int main() {}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2017-06-09 17:24:23 +03:00
|
|
|
using namespace boost::variant2;
|
|
|
|
|
|
|
|
|
|
struct X
|
|
|
|
|
{
|
|
|
|
|
int v;
|
|
|
|
|
X() = default;
|
|
|
|
|
constexpr X( int v ): v( v ) {}
|
|
|
|
|
constexpr operator int() const { return v; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Y
|
|
|
|
|
{
|
|
|
|
|
int v;
|
|
|
|
|
constexpr Y(): v() {}
|
|
|
|
|
constexpr Y( int v ): v( v ) {}
|
|
|
|
|
constexpr operator int() const { return v; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum E
|
|
|
|
|
{
|
|
|
|
|
v
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
|
|
|
|
|
|
2026-01-21 19:52:21 +02:00
|
|
|
template<std::size_t I, class V> constexpr variant_alternative_t<I, V> test( V&& v )
|
2017-06-09 17:24:23 +03:00
|
|
|
{
|
|
|
|
|
V v2( std::forward<V>(v) );
|
2026-01-21 19:52:21 +02:00
|
|
|
return get<I>( v2 );
|
2017-06-09 17:24:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<0>( variant<int>( 1 ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<0>( variant<X>( 1 ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<0>( variant<Y>( 1 ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<0>( variant<int, float>( 1 ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<1>( variant<int, float>( 3.0f ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 3.0f );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<2>( variant<int, int, float>( 3.0f ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 3.0f );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<2>( variant<E, E, X>( 1 ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<4>( variant<int, int, float, float, X>( X(1) ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<2>( variant<E, E, Y>( 1 ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-21 19:52:21 +02:00
|
|
|
constexpr auto w = test<4>( variant<int, int, float, float, Y>( Y(1) ) );
|
2017-06-09 17:24:23 +03:00
|
|
|
STATIC_ASSERT( w == 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2026-01-21 14:58:47 +02:00
|
|
|
|
|
|
|
|
#endif
|