forked from boostorg/variant2
Add test/get_by_type(_cx).cpp, get_by_index_cx.cpp
This commit is contained in:
@ -142,7 +142,7 @@ template<class U, class... T> constexpr U& get(variant<T...>& v)
|
||||
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
|
||||
constexpr auto I = mp_find<variant<T...>, U>::value;
|
||||
|
||||
return v.index() == I? v._get_impl( mp_size_t<I>() ): throw bad_variant_access();
|
||||
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
|
||||
}
|
||||
|
||||
template<class U, class... T> constexpr U&& get(variant<T...>&& v)
|
||||
@ -150,7 +150,7 @@ template<class U, class... T> constexpr U&& get(variant<T...>&& v)
|
||||
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
|
||||
constexpr auto I = mp_find<variant<T...>, U>::value;
|
||||
|
||||
return v.index() == I? std::move( v._get_impl( mp_size_t<I>() ) ): throw bad_variant_access();
|
||||
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
|
||||
}
|
||||
|
||||
template<class U, class... T> constexpr U const& get(variant<T...> const& v)
|
||||
@ -158,7 +158,7 @@ template<class U, class... T> constexpr U const& get(variant<T...> const& v)
|
||||
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
|
||||
constexpr auto I = mp_find<variant<T...>, U>::value;
|
||||
|
||||
return v.index() == I? v._get_impl( mp_size_t<I>() ): throw bad_variant_access();
|
||||
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
|
||||
}
|
||||
|
||||
template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
|
||||
@ -166,7 +166,7 @@ template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
|
||||
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
|
||||
constexpr auto I = mp_find<variant<T...>, U>::value;
|
||||
|
||||
return v.index() == I? std::move( v._get_impl( mp_size_t<I>() ) ): throw bad_variant_access();
|
||||
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
|
||||
}
|
||||
|
||||
// get_if
|
||||
|
@ -14,5 +14,8 @@ REQ = ; #[ requires cxx11_variadic_templates cxx11_template_aliases cxx11_declty
|
||||
run variant_size.cpp : : : $(REQ) ;
|
||||
run variant_alternative.cpp : : : $(REQ) ;
|
||||
run holds_alternative.cpp : : : $(REQ) ;
|
||||
run holds_alternative_cx.cpp : : : $(REQ) ;
|
||||
compile holds_alternative_cx.cpp : : : $(REQ) ;
|
||||
run get_by_index.cpp : : : $(REQ) ;
|
||||
compile get_by_index_cx.cpp : : : $(REQ) ;
|
||||
run get_by_type.cpp : : : $(REQ) ;
|
||||
compile get_by_type_cx.cpp : : : $(REQ) ;
|
||||
|
90
test/get_by_index_cx.cpp
Normal file
90
test/get_by_index_cx.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
|
||||
// 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;
|
||||
|
||||
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
constexpr variant<int> v;
|
||||
|
||||
STATIC_ASSERT( get<0>(v) == 0 );
|
||||
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int> v( 1 );
|
||||
|
||||
STATIC_ASSERT( get<0>(v) == 1 );
|
||||
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float> v;
|
||||
|
||||
STATIC_ASSERT( get<0>(v) == 0 );
|
||||
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
|
||||
|
||||
STATIC_ASSERT( get_if<1>(&v) == nullptr );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float> v( 1 );
|
||||
|
||||
STATIC_ASSERT( get<0>(v) == 1 );
|
||||
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
|
||||
|
||||
STATIC_ASSERT( get_if<1>(&v) == nullptr );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float> v( 3.14f );
|
||||
|
||||
STATIC_ASSERT( get_if<0>(&v) == nullptr );
|
||||
|
||||
STATIC_ASSERT( get<1>(v) == 3.14f );
|
||||
STATIC_ASSERT( get_if<1>(&v) == &get<1>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float, float> v;
|
||||
|
||||
STATIC_ASSERT( get<0>(v) == 0 );
|
||||
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
|
||||
|
||||
STATIC_ASSERT( get_if<1>(&v) == nullptr );
|
||||
|
||||
STATIC_ASSERT( get_if<2>(&v) == nullptr );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float, float> v( 1 );
|
||||
|
||||
STATIC_ASSERT( get<0>(v) == 1 );
|
||||
STATIC_ASSERT( get_if<0>(&v) == &get<0>(v) );
|
||||
|
||||
STATIC_ASSERT( get_if<1>(&v) == nullptr );
|
||||
|
||||
STATIC_ASSERT( get_if<2>(&v) == nullptr );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, int, float> v( 3.14f );
|
||||
|
||||
STATIC_ASSERT( get_if<0>(&v) == nullptr );
|
||||
|
||||
STATIC_ASSERT( get_if<1>(&v) == nullptr );
|
||||
|
||||
STATIC_ASSERT( get<2>(v) == 3.14f );
|
||||
STATIC_ASSERT( get_if<2>(&v) == &get<2>(v) );
|
||||
}
|
||||
}
|
166
test/get_by_type.cpp
Normal file
166
test/get_by_type.cpp
Normal file
@ -0,0 +1,166 @@
|
||||
|
||||
// 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>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
||||
using namespace boost::variant2;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
variant<int> v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int> const v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int const> v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int volatile> const v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(v)), int const volatile&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(std::move(v))), int const volatile&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int volatile>(&v)), int const volatile*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int> v;
|
||||
|
||||
BOOST_TEST_EQ( get<int>(v), 0 );
|
||||
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
variant<int> v( 1 );
|
||||
|
||||
BOOST_TEST_EQ( get<int>(v), 1 );
|
||||
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, float> v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, float> const v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float const&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float const&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float const*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int const, float volatile> v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float volatile&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float volatile&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float volatile*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int const, float volatile> const v;
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float const volatile&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float const volatile&&>));
|
||||
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float const volatile*>));
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, float> v;
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, float> v( 1 );
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
{
|
||||
variant<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) );
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, float, float> v;
|
||||
|
||||
BOOST_TEST_EQ( get<int>(v), 0 );
|
||||
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, float, float> v( 1 );
|
||||
|
||||
BOOST_TEST_EQ( get<int>(v), 1 );
|
||||
BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, int, float> v( 3.14f );
|
||||
|
||||
BOOST_TEST_EQ( get<float>(v), 3.14f );
|
||||
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
78
test/get_by_type_cx.cpp
Normal file
78
test/get_by_type_cx.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
|
||||
// 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;
|
||||
|
||||
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
constexpr variant<int> v;
|
||||
|
||||
STATIC_ASSERT( get<int>(v) == 0 );
|
||||
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int> v( 1 );
|
||||
|
||||
STATIC_ASSERT( get<int>(v) == 1 );
|
||||
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float> v;
|
||||
|
||||
STATIC_ASSERT( get<int>(v) == 0 );
|
||||
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
|
||||
|
||||
STATIC_ASSERT( get_if<float>(&v) == nullptr );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float> v( 1 );
|
||||
|
||||
STATIC_ASSERT( get<int>(v) == 1 );
|
||||
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
|
||||
|
||||
STATIC_ASSERT( get_if<float>(&v) == nullptr );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float> v( 3.14f );
|
||||
|
||||
STATIC_ASSERT( get_if<int>(&v) == nullptr );
|
||||
|
||||
STATIC_ASSERT( get<float>(v) == 3.14f );
|
||||
STATIC_ASSERT( get_if<float>(&v) == &get<float>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float, float> v;
|
||||
|
||||
STATIC_ASSERT( get<int>(v) == 0 );
|
||||
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, float, float> v( 1 );
|
||||
|
||||
STATIC_ASSERT( get<int>(v) == 1 );
|
||||
STATIC_ASSERT( get_if<int>(&v) == &get<int>(v) );
|
||||
}
|
||||
|
||||
{
|
||||
constexpr variant<int, int, float> v( 3.14f );
|
||||
|
||||
STATIC_ASSERT( get<float>(v) == 3.14f );
|
||||
STATIC_ASSERT( get_if<float>(&v) == &get<float>(v) );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user