Make relational operators constexpr; add test/variant_eq_ne_cx.cpp

This commit is contained in:
Peter Dimov
2019-03-06 00:26:55 +02:00
parent e5e09c1c04
commit 204bcce9df
3 changed files with 105 additions and 4 deletions

View File

@ -1562,7 +1562,7 @@ template<class... T> struct eq_L
variant<T...> const & v;
variant<T...> const & w;
template<class I> bool operator()( I i ) const
template<class I> constexpr bool operator()( I i ) const
{
return v._get_impl( i ) == w._get_impl( i );
}
@ -1583,7 +1583,7 @@ template<class... T> struct ne_L
variant<T...> const & v;
variant<T...> const & w;
template<class I> bool operator()( I i ) const
template<class I> constexpr bool operator()( I i ) const
{
return v._get_impl( i ) != w._get_impl( i );
}
@ -1604,7 +1604,7 @@ template<class... T> struct lt_L
variant<T...> const & v;
variant<T...> const & w;
template<class I> bool operator()( I i ) const
template<class I> constexpr bool operator()( I i ) const
{
return v._get_impl( i ) < w._get_impl( i );
}
@ -1630,7 +1630,7 @@ template<class... T> struct le_L
variant<T...> const & v;
variant<T...> const & w;
template<class I> bool operator()( I i ) const
template<class I> constexpr bool operator()( I i ) const
{
return v._get_impl( i ) <= w._get_impl( i );
}

View File

@ -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 ;

98
test/variant_eq_ne_cx.cpp Normal file
View File

@ -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 <boost/variant2/variant.hpp>
using namespace boost::variant2;
#if !defined(BOOST_MP11_HAS_CXX14_CONSTEXPR)
#include <boost/config/pragma_message.hpp>
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<int> 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<int, float> 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<int, int, float> 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<X> v1, v2;
STATIC_ASSERT( !(v1 == v2) );
STATIC_ASSERT( !(v1 != v2) );
}
}
#endif