forked from boostorg/variant2
Make emplace constexpr
This commit is contained in:
1 parent
e68ff1f014
commit
0997734e5c
4 files changed
+196
-25
No files matched your search
@@ -33,7 +33,9 @@ run variant_copy_assign.cpp : : : $(REQ) ;
|
||||
run variant_move_assign.cpp : : : $(REQ) ;
|
||||
run variant_value_assign.cpp : : : $(REQ) ;
|
||||
run variant_emplace_index.cpp : : : $(REQ) ;
|
||||
compile variant_emplace_index_cx.cpp : : : $(REQ) ;
|
||||
run variant_emplace_type.cpp : : : $(REQ) ;
|
||||
compile variant_emplace_type_cx.cpp : : : $(REQ) ;
|
||||
run variant_swap.cpp : : : $(REQ) ;
|
||||
run variant_eq_ne.cpp : : : $(REQ) ;
|
||||
run variant_destroy.cpp : : : $(REQ) ;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
|
||||
// Copyright 2017 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
|
||||
|
||||
#include <boost/variant2/variant.hpp>
|
||||
|
||||
using namespace boost::variant2;
|
||||
|
||||
struct X
|
||||
{
|
||||
int v;
|
||||
constexpr X(): v( 0 ) {}
|
||||
constexpr explicit X( int v ): v( v ) {}
|
||||
constexpr operator int() const { return v; }
|
||||
};
|
||||
|
||||
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
|
||||
|
||||
template<class V, std::size_t I, class A> constexpr A test( A const& a )
|
||||
{
|
||||
V v;
|
||||
|
||||
v.template emplace<I>( a );
|
||||
|
||||
return get<I>(v);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
constexpr auto w = test<variant<int>, 0>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<X>, 0>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, float>, 0>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, float>, 1>( 3.0f );
|
||||
STATIC_ASSERT( w == 3.0f );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float, float, X, X>, 0>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float, float, X, X>, 1>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float, float, X, X>, 2>( 2.0f );
|
||||
STATIC_ASSERT( w == 2.0f );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float, float, X, X>, 3>( 3.0f );
|
||||
STATIC_ASSERT( w == 3.0f );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float, float, X, X>, 4>( 4 );
|
||||
STATIC_ASSERT( w == 4 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float, float, X, X>, 5>( 5 );
|
||||
STATIC_ASSERT( w == 5 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
// Copyright 2017 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
|
||||
|
||||
#include <boost/variant2/variant.hpp>
|
||||
|
||||
using namespace boost::variant2;
|
||||
|
||||
struct X
|
||||
{
|
||||
int v;
|
||||
constexpr X(): v( 0 ) {}
|
||||
constexpr explicit X( int v ): v( v ) {}
|
||||
constexpr operator int() const { return v; }
|
||||
};
|
||||
|
||||
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
|
||||
|
||||
template<class V, class T, class A> constexpr A test( A const& a )
|
||||
{
|
||||
V v;
|
||||
|
||||
v.template emplace<T>( a );
|
||||
|
||||
return get<T>(v);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
constexpr auto w = test<variant<int>, int>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<X>, X>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, float>, int>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, float>, float>( 3.0f );
|
||||
STATIC_ASSERT( w == 3.0f );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float>, float>( 3.0f );
|
||||
STATIC_ASSERT( w == 3.0f );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr auto w = test<variant<int, int, float, float, X>, X>( 1 );
|
||||
STATIC_ASSERT( w == 1 );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user