| 1 | |
| 2 | // Copyright 2020 Peter Dimov. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // https://www.boost.org/LICENSE_1_0.txt |
| 5 | |
| 6 | #if defined(_MSC_VER) |
| 7 | # pragma warning( disable: 4503 ) // decorated name length exceeded |
| 8 | #endif |
| 9 | |
| 10 | #include <boost/variant2/variant.hpp> |
| 11 | #include <boost/mp11.hpp> |
| 12 | #include <boost/core/lightweight_test.hpp> |
| 13 | |
| 14 | using namespace boost::mp11; |
| 15 | |
| 16 | template<class I> struct X |
| 17 | { |
| 18 | static int const value = I::value; |
| 19 | int v_; |
| 20 | }; |
| 21 | |
| 22 | template<class I> int const X<I>::value; |
| 23 | |
| 24 | template<class I> struct Y |
| 25 | { |
| 26 | static int const value = I::value; |
| 27 | int v_; |
| 28 | |
| 29 | Y() = default; |
| 30 | Y( Y const& ) = default; |
| 31 | |
| 32 | explicit Y( int v ): v_( v ) {} |
| 33 | |
| 34 | Y& operator=( Y const& ) noexcept = default; |
| 35 | |
| 36 | Y& operator=( Y&& r ) noexcept |
| 37 | { |
| 38 | v_ = r.v_; |
| 39 | return *this; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | template<class I> int const Y<I>::value; |
| 44 | |
| 45 | template<class I> struct Z |
| 46 | { |
| 47 | static int const value = I::value; |
| 48 | int v_; |
| 49 | |
| 50 | ~Z() {} |
| 51 | }; |
| 52 | |
| 53 | template<class I> int const Z<I>::value; |
| 54 | |
| 55 | template<class V> struct F1 |
| 56 | { |
| 57 | template<class T> void operator()( T ) const |
| 58 | { |
| 59 | int const i = T::value; |
| 60 | |
| 61 | T t{ i * 2 }; |
| 62 | |
| 63 | using boost::variant2::get; |
| 64 | |
| 65 | { |
| 66 | V v( t ); |
| 67 | |
| 68 | BOOST_TEST_EQ( v.index(), i ); |
| 69 | BOOST_TEST_EQ( get<i>( v ).v_, t.v_ ); |
| 70 | BOOST_TEST_EQ( get<T>( v ).v_, t.v_ ); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | V const v( t ); |
| 75 | |
| 76 | BOOST_TEST_EQ( v.index(), i ); |
| 77 | BOOST_TEST_EQ( get<i>( v ).v_, t.v_ ); |
| 78 | BOOST_TEST_EQ( get<T>( v ).v_, t.v_ ); |
| 79 | } |
| 80 | |
| 81 | { |
| 82 | V v; |
| 83 | |
| 84 | v = t; |
| 85 | |
| 86 | BOOST_TEST_EQ( v.index(), i ); |
| 87 | BOOST_TEST_EQ( get<i>( v ).v_, t.v_ ); |
| 88 | BOOST_TEST_EQ( get<T>( v ).v_, t.v_ ); |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | template<class V> void test() |
| 94 | { |
| 95 | mp_for_each<V>( F1<V>() ); |
| 96 | } |
| 97 | |
| 98 | int main() |
| 99 | { |
| 100 | int const N = 32; |
| 101 | |
| 102 | using V = mp_rename<mp_iota_c<N>, boost::variant2::variant>; |
| 103 | |
| 104 | test< mp_transform<X, V> >(); |
| 105 | test< mp_transform<Y, V> >(); |
| 106 | test< mp_transform<Z, V> >(); |
| 107 | |
| 108 | return boost::report_errors(); |
| 109 | } |
| 110 | |