mirror of
https://github.com/boostorg/variant2.git
synced 2025-08-02 13:44:27 +02:00
Add test/variant_in_place_index_construct(_cx)
This commit is contained in:
@@ -592,12 +592,32 @@ template<class T> struct in_place_type_t
|
|||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<class T> constexpr in_place_type_t<T> in_place_type{};
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
template<class T> struct is_in_place_type: std::false_type {};
|
||||||
|
template<class T> struct is_in_place_type<in_place_type_t<T>>: std::true_type {};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
// in_place_index_t
|
// in_place_index_t
|
||||||
|
|
||||||
template<std::size_t I> struct in_place_index_t
|
template<std::size_t I> struct in_place_index_t
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<std::size_t I> constexpr in_place_index_t<I> in_place_index{};
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
template<class T> struct is_in_place_index: std::false_type {};
|
||||||
|
template<std::size_t I> struct is_in_place_index<in_place_index_t<I>>: std::true_type {};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
// variant
|
// variant
|
||||||
|
|
||||||
template<class... T> class variant: private variant2::detail::variant_base<T...>
|
template<class... T> class variant: private variant2::detail::variant_base<T...>
|
||||||
@@ -643,7 +663,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<class U,
|
template<class U,
|
||||||
class E1 = std::enable_if_t<!std::is_same<std::decay_t<U>, variant>::value>,
|
class Ud = std::decay_t<U>,
|
||||||
|
class E1 = std::enable_if_t< !std::is_same<Ud, variant>::value && !variant2::detail::is_in_place_index<Ud>::value && !variant2::detail::is_in_place_type<Ud>::value >,
|
||||||
class V = variant2::detail::resolve_overload_type<U&&, T...>,
|
class V = variant2::detail::resolve_overload_type<U&&, T...>,
|
||||||
class E2 = std::enable_if_t<std::is_constructible<V, U>::value>
|
class E2 = std::enable_if_t<std::is_constructible<V, U>::value>
|
||||||
>
|
>
|
||||||
|
@@ -25,3 +25,5 @@ run variant_copy_construct.cpp : : : $(REQ) ;
|
|||||||
run variant_move_construct.cpp : : : $(REQ) ;
|
run variant_move_construct.cpp : : : $(REQ) ;
|
||||||
run variant_value_construct.cpp : : : $(REQ) ;
|
run variant_value_construct.cpp : : : $(REQ) ;
|
||||||
compile variant_value_construct_cx.cpp : : : $(REQ) ;
|
compile variant_value_construct_cx.cpp : : : $(REQ) ;
|
||||||
|
run variant_in_place_index_construct.cpp : : : $(REQ) ;
|
||||||
|
compile variant_in_place_index_construct_cx.cpp : : : $(REQ) ;
|
||||||
|
124
test/variant_in_place_index_construct.cpp
Normal file
124
test/variant_in_place_index_construct.cpp
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
struct X
|
||||||
|
{
|
||||||
|
X() = default;
|
||||||
|
X( in_place_index_t<0> ) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
variant<int> v( in_place_index<0> );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 0 );
|
||||||
|
BOOST_TEST_EQ( get<0>(v), 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<X> v( in_place_index<0> );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int> v( in_place_index<0>, 1 );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 0 );
|
||||||
|
BOOST_TEST_EQ( get<0>(v), 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, float> v( in_place_index<0> );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 0 );
|
||||||
|
BOOST_TEST_EQ( get<0>(v), 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, float> v( in_place_index<0>, 1 );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 0 );
|
||||||
|
BOOST_TEST_EQ( get<0>(v), 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, float> v( in_place_index<1> );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 1 );
|
||||||
|
BOOST_TEST_EQ( get<1>(v), 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, float> v( in_place_index<1>, 3.14f );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 1 );
|
||||||
|
BOOST_TEST_EQ( get<1>(v), 3.14f );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float, float, std::string, std::string> v( in_place_index<0>, 1 );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 0 );
|
||||||
|
BOOST_TEST_EQ( get<0>(v), 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float, float, std::string, std::string> v( in_place_index<1>, 1 );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 1 );
|
||||||
|
BOOST_TEST_EQ( get<1>(v), 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float, float, std::string, std::string> v( in_place_index<2>, 3.14f );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 2 );
|
||||||
|
BOOST_TEST_EQ( get<2>(v), 3.14f );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float, float, std::string, std::string> v( in_place_index<3>, 3.14f );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 3 );
|
||||||
|
BOOST_TEST_EQ( get<3>(v), 3.14f );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float, float, std::string, std::string> v( in_place_index<4>, "text" );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 4 );
|
||||||
|
BOOST_TEST_EQ( get<4>(v), std::string("text") );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float, float, std::string, std::string> v( in_place_index<5>, "text" );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 5 );
|
||||||
|
BOOST_TEST_EQ( get<5>(v), std::string("text") );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
variant<int, int, float, float, std::string, std::string> v( in_place_index<5>, 4, 'a' );
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( v.index(), 5 );
|
||||||
|
BOOST_TEST_EQ( get<5>(v), std::string( 4, 'a' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
116
test/variant_in_place_index_construct_cx.cpp
Normal file
116
test/variant_in_place_index_construct_cx.cpp
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
struct X
|
||||||
|
{
|
||||||
|
constexpr X() = default;
|
||||||
|
constexpr explicit X(int, int) {}
|
||||||
|
X( in_place_index_t<0> ) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
constexpr variant<int> v( in_place_index<0> );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 0 );
|
||||||
|
STATIC_ASSERT( get<0>(v) == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<X> v( in_place_index<0> );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int> v( in_place_index<0>, 1 );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 0 );
|
||||||
|
STATIC_ASSERT( get<0>(v) == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, float> v( in_place_index<0> );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 0 );
|
||||||
|
STATIC_ASSERT( get<0>(v) == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, float> v( in_place_index<0>, 1 );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 0 );
|
||||||
|
STATIC_ASSERT( get<0>(v) == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, float> v( in_place_index<1> );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 1 );
|
||||||
|
STATIC_ASSERT( get<1>(v) == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, float> v( in_place_index<1>, 3.14f );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 1 );
|
||||||
|
STATIC_ASSERT( get<1>(v) == 3.14f );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, int, float, float, X, X> v( in_place_index<0>, 1 );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 0 );
|
||||||
|
STATIC_ASSERT( get<0>(v) == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, int, float, float, X, X> v( in_place_index<1>, 1 );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 1 );
|
||||||
|
STATIC_ASSERT( get<1>(v) == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, int, float, float, X, X> v( in_place_index<2>, 3.14f );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 2 );
|
||||||
|
STATIC_ASSERT( get<2>(v) == 3.14f );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, int, float, float, X, X> v( in_place_index<3>, 3.14f );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 3 );
|
||||||
|
STATIC_ASSERT( get<3>(v) == 3.14f );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, int, float, float, X, X> v( in_place_index<4> );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 4 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
constexpr variant<int, int, float, float, X, X> v( in_place_index<5>, 0, 0 );
|
||||||
|
|
||||||
|
STATIC_ASSERT( v.index() == 5 );
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user