From 204bcce9df03cedcb298aa916d0b3c64bec28b56 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 6 Mar 2019 00:26:55 +0200 Subject: [PATCH] Make relational operators constexpr; add test/variant_eq_ne_cx.cpp --- include/boost/variant2/variant.hpp | 8 +-- test/Jamfile | 3 + test/variant_eq_ne_cx.cpp | 98 ++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 test/variant_eq_ne_cx.cpp diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 7ecc25f..3cf2dc1 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -1562,7 +1562,7 @@ template struct eq_L variant const & v; variant const & w; - template bool operator()( I i ) const + template constexpr bool operator()( I i ) const { return v._get_impl( i ) == w._get_impl( i ); } @@ -1583,7 +1583,7 @@ template struct ne_L variant const & v; variant const & w; - template bool operator()( I i ) const + template constexpr bool operator()( I i ) const { return v._get_impl( i ) != w._get_impl( i ); } @@ -1604,7 +1604,7 @@ template struct lt_L variant const & v; variant const & w; - template bool operator()( I i ) const + template constexpr bool operator()( I i ) const { return v._get_impl( i ) < w._get_impl( i ); } @@ -1630,7 +1630,7 @@ template struct le_L variant const & v; variant const & w; - template bool operator()( I i ) const + template constexpr bool operator()( I i ) const { return v._get_impl( i ) <= w._get_impl( i ); } diff --git a/test/Jamfile b/test/Jamfile index 714f5e2..89be6a9 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -57,7 +57,10 @@ run variant_emplace_type.cpp ; compile variant_emplace_type_cx.cpp : [ requires cxx14_constexpr ] ; run variant_swap.cpp ; + run variant_eq_ne.cpp ; +compile variant_eq_ne_cx.cpp : [ requires cxx14_constexpr ] ; + run variant_destroy.cpp ; run variant_visit.cpp ; run variant_lt_gt.cpp ; diff --git a/test/variant_eq_ne_cx.cpp b/test/variant_eq_ne_cx.cpp new file mode 100644 index 0000000..9ebb751 --- /dev/null +++ b/test/variant_eq_ne_cx.cpp @@ -0,0 +1,98 @@ + +// Copyright 2017, 2019 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; + +#if !defined(BOOST_MP11_HAS_CXX14_CONSTEXPR) + +#include + +BOOST_PRAGMA_MESSAGE("Skipping constexpr op==, op!= test because BOOST_MP11_HAS_CXX14_CONSTEXPR is not defined") + +int main() {} + +#else + +struct X +{ +}; + +inline constexpr bool operator==( X const&, X const& ) { return false; } +inline constexpr bool operator!=( X const&, X const& ) { return false; } + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v1, v2, v3( 1 ), v4( 1 ); + + STATIC_ASSERT( v1 == v2 ); + STATIC_ASSERT( !(v1 != v2) ); + + STATIC_ASSERT( v1 != v3 ); + STATIC_ASSERT( !(v1 == v3) ); + + STATIC_ASSERT( v3 == v4 ); + STATIC_ASSERT( !(v3 != v4) ); + } + + { + constexpr variant v1, v2, v3( 1 ), v4( 1 ), v5( 3.14f ), v6( 3.14f ); + + STATIC_ASSERT( v1 == v2 ); + STATIC_ASSERT( !(v1 != v2) ); + + STATIC_ASSERT( v1 != v3 ); + STATIC_ASSERT( !(v1 == v3) ); + + STATIC_ASSERT( v3 == v4 ); + STATIC_ASSERT( !(v3 != v4) ); + + STATIC_ASSERT( v1 != v5 ); + STATIC_ASSERT( !(v1 == v5) ); + + STATIC_ASSERT( v3 != v5 ); + STATIC_ASSERT( !(v3 == v5) ); + + STATIC_ASSERT( v5 == v6 ); + STATIC_ASSERT( !(v5 != v6) ); + } + + { + constexpr variant v1, v2, v3( in_place_index_t<1>{} ), v4( in_place_index_t<1>{} ), v5( 3.14f ), v6( 3.14f ); + + STATIC_ASSERT( v1 == v2 ); + STATIC_ASSERT( !(v1 != v2) ); + + STATIC_ASSERT( v1 != v3 ); + STATIC_ASSERT( !(v1 == v3) ); + + STATIC_ASSERT( v3 == v4 ); + STATIC_ASSERT( !(v3 != v4) ); + + STATIC_ASSERT( v1 != v5 ); + STATIC_ASSERT( !(v1 == v5) ); + + STATIC_ASSERT( v3 != v5 ); + STATIC_ASSERT( !(v3 == v5) ); + + STATIC_ASSERT( v5 == v6 ); + STATIC_ASSERT( !(v5 != v6) ); + } + + { + constexpr variant v1, v2; + + STATIC_ASSERT( !(v1 == v2) ); + STATIC_ASSERT( !(v1 != v2) ); + } +} + +#endif