diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 4a0937c..260411c 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -283,6 +283,35 @@ template constexpr std::add_pointer_t get_if(varia namespace detail { +// trivially_* + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 + +template struct is_trivially_copy_constructible: mp_bool::value && std::has_trivial_copy_constructor::value> +{ +}; + +template struct is_trivially_copy_assignable: mp_bool::value && std::has_trivial_copy_assign::value> +{ +}; + +template struct is_trivially_move_constructible: mp_bool::value && std::is_trivial::value> +{ +}; + +template struct is_trivially_move_assignable: mp_bool::value && std::is_trivial::value> +{ +}; + +#else + +using std::is_trivially_copy_constructible; +using std::is_trivially_copy_assignable; +using std::is_trivially_move_constructible; +using std::is_trivially_move_assignable; + +#endif + // variant_storage template union variant_storage_impl; @@ -311,12 +340,12 @@ template union variant_storage_impl { } - template void emplace( mp_size_t<0>, A&&... a ) noexcept + template void emplace( mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } - template void emplace( mp_size_t, A&&... a ) noexcept + template void emplace( mp_size_t, A&&... a ) { rest_.emplace( mp_size_t(), std::forward(a)... ); } @@ -342,16 +371,26 @@ template union variant_storage_impl { } - template void emplace( mp_size_t<0>, A&&... a ) noexcept + template void emplace_impl( mp_false, mp_size_t<0>, A&&... a ) { ::new( &first_ ) T1( std::forward(a)... ); } - template void emplace( mp_size_t, A&&... a ) noexcept + template constexpr void emplace_impl( mp_false, mp_size_t, A&&... a ) { rest_.emplace( mp_size_t(), std::forward(a)... ); } + template constexpr void emplace_impl( mp_true, mp_size_t, A&&... a ) noexcept + { + *this = variant_storage_impl( mp_size_t(), std::forward(a)... ); + } + + template constexpr void emplace( mp_size_t, A&&... a ) + { + this->emplace_impl( mp_all, variant2::detail::is_trivially_move_assignable...>(), mp_size_t(), std::forward(a)... ); + } + constexpr T1& get( mp_size_t<0> ) noexcept { return first_; } constexpr T1 const& get( mp_size_t<0> ) const noexcept { return first_; } @@ -436,20 +475,25 @@ template struct variant_base_impl return st1_.get( mp_size_t() ); } - template void emplace( A&&... a ) + template constexpr void emplace_impl( mp_true, mp_bool, A&&... a ) { - std::size_t const J = I+1; + st1_.emplace( mp_size_t(), std::forward(a)... ); + ix_ = J; + } + template constexpr void emplace_impl( mp_false, mp_true, A&&... a ) + { + U tmp( std::forward(a)... ); + + st1_.emplace( mp_size_t(), std::move(tmp) ); + ix_ = J; + } + + template void emplace_impl( mp_false, mp_false, A&&... a ) + { std::size_t const K = mp_find_if, std::is_nothrow_constructible>::value; - using U = mp_at_c, I>; - - if( std::is_nothrow_constructible::value ) - { - st1_.emplace( mp_size_t(), std::forward(a)... ); - ix_ = J; - } - else if( K < sizeof...(T) ) // have nothrow destructible + if( K < sizeof...(T) ) // have nothrow destructible { try { @@ -458,8 +502,8 @@ template struct variant_base_impl } catch( ... ) { - st1_.emplace( mp_size_t() ); - ix_ = K; + st1_.emplace( mp_size_t() ); + ix_ = K+1; throw; } @@ -474,6 +518,14 @@ template struct variant_base_impl ix_ = J; } } + + template constexpr void emplace( A&&... a ) + { + std::size_t const J = I+1; + using U = mp_at_c, I>; + + this->emplace_impl( std::is_nothrow_constructible(), mp_all, variant2::detail::is_trivially_move_assignable...>(), std::forward(a)... ); + } }; // trivially destructible, double buffered @@ -516,7 +568,7 @@ template struct variant_base_impl return ix_ >= 0? st1_.get( j ): st2_.get( j ); } - template void emplace( A&&... a ) + template constexpr void emplace( A&&... a ) { size_t const J = I+1; @@ -829,7 +881,18 @@ public: { } - template...>, E1>> + template...>, E1> + > + constexpr variant( variant const& r ) noexcept + : variant_base( static_cast(r) ) + { + } + + template...>>, E1>, + class E3 = mp_if...>, E1> + > variant( variant const& r ) noexcept( mp_all...>::value ) { @@ -840,7 +903,18 @@ public: }); } - template...>, E1>> + template...>, E1> + > + constexpr variant( variant && r ) noexcept + : variant_base( static_cast(r) ) + { + } + + template...>>, E1>, + class E3 = mp_if...>, E1> + > variant( variant && r ) noexcept( mp_all...>::value ) { @@ -884,8 +958,20 @@ public: } // assignment - template..., std::is_copy_assignable...>, E1>> - variant& operator=( variant const & r ) + template..., variant2::detail::is_trivially_copy_assignable...>, E1> + > + constexpr variant& operator=( variant const & r ) noexcept + { + static_cast( *this ) = static_cast( r ); + return *this; + } + + template..., variant2::detail::is_trivially_copy_assignable...>>, E1>, + class E3 = mp_if..., std::is_copy_assignable...>, E1> + > + constexpr variant& operator=( variant const & r ) noexcept( mp_all..., std::is_nothrow_copy_assignable...>::value ) { mp_with_index( r.index(), [&]( auto I ){ @@ -904,7 +990,19 @@ public: return *this; } - template..., std::is_move_assignable...>, E1>> + template..., variant2::detail::is_trivially_move_assignable...>, E1> + > + constexpr variant& operator=( variant && r ) noexcept + { + static_cast( *this ) = static_cast( r ); + return *this; + } + + template..., variant2::detail::is_trivially_move_assignable...>>, E1>, + class E3 = mp_if..., std::is_move_assignable...>, E1> + > variant& operator=( variant && r ) noexcept( mp_all..., std::is_nothrow_move_assignable...>::value ) { @@ -929,7 +1027,7 @@ public: class V = variant2::detail::resolve_overload_type, class E2 = std::enable_if_t::value && std::is_constructible::value> > - variant& operator=( U&& u ) + constexpr variant& operator=( U&& u ) noexcept( std::is_nothrow_assignable::value && std::is_nothrow_constructible::value ) { std::size_t const I = variant2::detail::resolve_overload_index::value; @@ -949,28 +1047,28 @@ public: // modifiers template, U>, class E = std::enable_if_t::value>> - U& emplace( A&&... a ) + constexpr U& emplace( A&&... a ) { variant_base::template emplace( std::forward(a)... ); return _get_impl( I() ); } template, U>, class E = std::enable_if_t&, A...>::value>> - U& emplace( std::initializer_list il, A&&... a ) + constexpr U& emplace( std::initializer_list il, A&&... a ) { variant_base::template emplace( il, std::forward(a)... ); return _get_impl( I() ); } template, I>, A...>::value>> - variant_alternative_t>& emplace( A&&... a ) + constexpr variant_alternative_t>& emplace( A&&... a ) { variant_base::template emplace( std::forward(a)... ); return _get_impl( mp_size_t() ); } template, I>, std::initializer_list&, A...>::value>> - variant_alternative_t>& emplace( std::initializer_list il, A&&... a ) + constexpr variant_alternative_t>& emplace( std::initializer_list il, A&&... a ) { variant_base::template emplace( il, std::forward(a)... ); return _get_impl( mp_size_t() ); @@ -1044,7 +1142,7 @@ public: private: - template> static variant _subset_impl( mp_size_t, V && v ) + template> static constexpr variant _subset_impl( mp_size_t, V && v ) { return variant( in_place_index, std::forward(v) ); } diff --git a/test/Jamfile b/test/Jamfile index c11eef5..39973c9 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -13,27 +13,49 @@ REQ = ; #[ requires cxx11_variadic_templates cxx11_template_aliases cxx11_declty run variant_size.cpp : : : $(REQ) ; run variant_alternative.cpp : : : $(REQ) ; + run variant_holds_alternative.cpp : : : $(REQ) ; compile variant_holds_alternative_cx.cpp : : : $(REQ) ; + run variant_get_by_index.cpp : : : $(REQ) ; compile variant_get_by_index_cx.cpp : : : $(REQ) ; + run variant_get_by_type.cpp : : : $(REQ) ; compile variant_get_by_type_cx.cpp : : : $(REQ) ; + run variant_default_construct.cpp : : : $(REQ) ; compile variant_default_construct_cx.cpp : : : $(REQ) ; + run variant_copy_construct.cpp : : : $(REQ) ; +compile variant_copy_construct_cx.cpp : : : $(REQ) ; + run variant_move_construct.cpp : : : $(REQ) ; +compile variant_move_construct_cx.cpp : : : $(REQ) ; + run variant_value_construct.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) ; + run variant_in_place_type_construct.cpp : : : $(REQ) ; compile variant_in_place_type_construct_cx.cpp : : : $(REQ) ; + run variant_copy_assign.cpp : : : $(REQ) ; +compile variant_copy_assign_cx.cpp : : : $(REQ) ; + run variant_move_assign.cpp : : : $(REQ) ; +compile variant_move_assign_cx.cpp : : : $(REQ) ; + run variant_value_assign.cpp : : : $(REQ) ; +compile variant_value_assign_cx.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) ; diff --git a/test/variant_copy_assign_cx.cpp b/test/variant_copy_assign_cx.cpp new file mode 100644 index 0000000..8be2579 --- /dev/null +++ b/test/variant_copy_assign_cx.cpp @@ -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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( A const& a ) +{ + V v; + + v = a; + + return get(v); +} + +int main() +{ + { + constexpr variant v( 1 ); + constexpr auto w = test, int>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test, X>( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test, Y>( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr variant v( 1 ); + constexpr auto w = test, int>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test, float>( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test, float>( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test, X>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( X(1) ); + constexpr auto w = test, X>( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test, Y>( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( Y(1) ); + constexpr auto w = test, Y>( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/test/variant_copy_construct_cx.cpp b/test/variant_copy_construct_cx.cpp new file mode 100644 index 0000000..e208f4e --- /dev/null +++ b/test/variant_copy_construct_cx.cpp @@ -0,0 +1,122 @@ + +// 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 + +#if !defined( __cpp_constexpr ) || __cpp_constexpr < 201603 + +// no constexpr lambda support +int main() {} + +#else + +#include + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( V const& v ) +{ + V v2( v ); + return get(v); +} + +int main() +{ + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 3.0f ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( X(1) ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr variant v( 1 ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr variant v( Y(1) ); + constexpr auto w = test( v ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} + +#endif // constexpr lambda support diff --git a/test/variant_emplace_index_cx.cpp b/test/variant_emplace_index_cx.cpp new file mode 100644 index 0000000..f66f99f --- /dev/null +++ b/test/variant_emplace_index_cx.cpp @@ -0,0 +1,136 @@ + +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr explicit X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr explicit Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr A test( A const& a ) +{ + V v; + + v.template emplace( a ); + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 1>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 1>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 2>( 2.0f ); + STATIC_ASSERT( w == 2.0f ); + } + + { + constexpr auto w = test, 3>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, 4>( 4 ); + STATIC_ASSERT( w == 4 ); + } + + { + constexpr auto w = test, 5>( 5 ); + STATIC_ASSERT( w == 5 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, 0>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 1>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, 2>( 2.0f ); + STATIC_ASSERT( w == 2.0f ); + } + + { + constexpr auto w = test, 3>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, 4>( 4 ); + STATIC_ASSERT( w == 4 ); + } + + { + constexpr auto w = test, 5>( 5 ); + STATIC_ASSERT( w == 5 ); + } + +#endif +} diff --git a/test/variant_emplace_type_cx.cpp b/test/variant_emplace_type_cx.cpp new file mode 100644 index 0000000..2449b12 --- /dev/null +++ b/test/variant_emplace_type_cx.cpp @@ -0,0 +1,91 @@ + +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr explicit X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr explicit Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr A test( A const& a ) +{ + V v; + + v.template emplace( a ); + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/test/variant_move_assign_cx.cpp b/test/variant_move_assign_cx.cpp new file mode 100644 index 0000000..d7ab65f --- /dev/null +++ b/test/variant_move_assign_cx.cpp @@ -0,0 +1,107 @@ + +// 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 +#include + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( A&& a ) +{ + V v; + + v = std::forward(a); + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, int>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, int>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, float>( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, float>( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, X>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( variant( X(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, Y>( variant( Y(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} diff --git a/test/variant_move_construct_cx.cpp b/test/variant_move_construct_cx.cpp new file mode 100644 index 0000000..269607c --- /dev/null +++ b/test/variant_move_construct_cx.cpp @@ -0,0 +1,113 @@ + +// 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 + +#if !defined( __cpp_constexpr ) || __cpp_constexpr < 201603 + +// no constexpr lambda support +int main() {} + +#else + +#include +#include + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr T test( V&& v ) +{ + V v2( std::forward(v) ); + return get(v); +} + +int main() +{ + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test( variant( 3.0f ) ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( X(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test( variant( 1 ) ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test( variant( Y(1) ) ); + STATIC_ASSERT( w == 1 ); + } + +#endif +} + +#endif // constexpr lambda support diff --git a/test/variant_value_assign_cx.cpp b/test/variant_value_assign_cx.cpp new file mode 100644 index 0000000..0a29a0a --- /dev/null +++ b/test/variant_value_assign_cx.cpp @@ -0,0 +1,106 @@ + +// 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 + +using namespace boost::variant2; + +struct X +{ + int v; + X() = default; + constexpr X( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +struct Y +{ + int v; + constexpr Y(): v() {} + constexpr Y( int v ): v( v ) {} + constexpr operator int() const { return v; } +}; + +enum E +{ + v +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +template constexpr A test( A const& a ) +{ + V v; + + v = a; + + return get(v); +} + +int main() +{ + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + +#endif + + { + constexpr auto w = test, int>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, float>( 3.0f ); + STATIC_ASSERT( w == 3.0f ); + } + + { + constexpr auto w = test, X>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, X>( X(1) ); + STATIC_ASSERT( w == 1 ); + } + +#if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000 +#else + + { + constexpr auto w = test, Y>( 1 ); + STATIC_ASSERT( w == 1 ); + } + + { + constexpr auto w = test, Y>( Y(1) ); + STATIC_ASSERT( w == 1 ); + } + +#endif +}