diff --git a/test/Jamfile b/test/Jamfile index 4a5f6fe..6939dd5 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -143,3 +143,5 @@ run variant_derived_construct2.cpp ; compile variant_visit_cx.cpp : [ requires cxx14_constexpr ] ; compile variant_visit_cx_2.cpp : [ requires cxx14_constexpr ] ; compile variant_visit_r_cx.cpp : [ requires cxx14_constexpr ] ; + +run variant_index_type.cpp : : : release ; diff --git a/test/variant_index_type.cpp b/test/variant_index_type.cpp new file mode 100644 index 0000000..94a532e --- /dev/null +++ b/test/variant_index_type.cpp @@ -0,0 +1,92 @@ +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(_MSC_VER) +# pragma warning( disable: 4503 ) // decorated name length exceeded +#endif + +#include +#include +#include + +using namespace boost::mp11; + +template struct X +{ +}; + +template using Xf = X< N::value >; + +template struct Y +{ + Y() = default; + Y( Y&& ) {} +}; + +template using Yf = Y< N::value >; + +template struct Z +{ + ~Z() {} +}; + +template using Zf = Z< N::value >; + +template struct W +{ + ~W() {} + + W() = default; + W( W&& ) {} +}; + +template using Wf = W< N::value >; + +template class F, std::size_t N> void test_() +{ + using V = mp_rename< mp_transform>, boost::variant2::variant >; + + { + V v( mp_back{} ); + BOOST_TEST_EQ( v.index(), mp_size() - 1 ); + } + + { + V v; + BOOST_TEST_EQ( v.index(), 0 ); + + v.template emplace< mp_back >(); + BOOST_TEST_EQ( v.index(), mp_size() - 1 ); + } +} + +template class F> void test() +{ + test_(); + test_(); + test_(); + test_(); + + test_(); + test_(); + test_(); + test_(); +} + +int main() +{ + static_assert( !boost::variant2::variant< X<0>, X<1> >::uses_double_storage(), "" ); + test(); + + static_assert( boost::variant2::variant< Y<0>, Y<1> >::uses_double_storage(), "" ); + test(); + + static_assert( !boost::variant2::variant< Z<0>, Z<1> >::uses_double_storage(), "" ); + test(); + + static_assert( boost::variant2::variant< W<0>, W<1> >::uses_double_storage(), "" ); + test(); + + return boost::report_errors(); +}