1
0
forked from boostorg/bind

Relational operators added

[SVN r27669]
This commit is contained in:
Peter Dimov
2005-03-15 14:15:38 +00:00
parent 7b9a97a758
commit ed817c5b15
4 changed files with 128 additions and 0 deletions

View File

@@ -1149,6 +1149,35 @@ template<class R, class F, class L>
return bind_t<bool, logical_not, list_type> ( logical_not(), list_type(f) );
}
// relational operators
#define BOOST_BIND_OPERATOR( op, name ) \
\
struct name \
{ \
template<class V, class W> bool operator()(V const & v, W const & w) const { return v op w; } \
}; \
\
template<class R, class F, class L, class A2> \
bind_t< bool, name, list2< bind_t<R, F, L>, typename add_value<A2>::type > > \
operator op (bind_t<R, F, L> const & f, A2 a2) \
{ \
typedef typename add_value<A2>::type B2; \
typedef list2< bind_t<R, F, L>, B2> list_type; \
return bind_t<bool, name, list_type> ( name(), list_type(f, a2) ); \
}
BOOST_BIND_OPERATOR( ==, equal )
BOOST_BIND_OPERATOR( !=, not_equal )
BOOST_BIND_OPERATOR( <, less )
BOOST_BIND_OPERATOR( <=, less_equal )
BOOST_BIND_OPERATOR( >, greater )
BOOST_BIND_OPERATOR( >=, greater_equal )
#undef BOOST_BIND_OPERATOR
} // namespace _bi
// visit_each

View File

@@ -24,6 +24,7 @@ DEPENDS all : bind ;
[ run bind_cv_test.cpp ]
[ run bind_stateful_test.cpp ]
[ run bind_not_test.cpp ]
[ run bind_rel_test.cpp ]
[ run mem_fn_test.cpp ]
[ run mem_fn_void_test.cpp ]
[ run mem_fn_derived_test.cpp ]

View File

@@ -18,6 +18,7 @@ test-suite "bind"
[ run bind_cv_test.cpp ]
[ run bind_stateful_test.cpp ]
[ run bind_not_test.cpp ]
[ run bind_rel_test.cpp ]
[ run mem_fn_test.cpp ]
[ run mem_fn_void_test.cpp ]
[ run mem_fn_derived_test.cpp ]

97
test/bind_rel_test.cpp Normal file
View File

@@ -0,0 +1,97 @@
#include <boost/config.hpp>
#if defined(BOOST_MSVC)
#pragma warning(disable: 4786) // identifier truncated in debug info
#pragma warning(disable: 4710) // function not inlined
#pragma warning(disable: 4711) // function selected for automatic inline expansion
#pragma warning(disable: 4514) // unreferenced inline removed
#endif
//
// bind_rel_test.cpp - ==, !=, <, <=, >, >= operators
//
// Copyright (c) 2005 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/bind.hpp>
#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
#pragma warning(push, 3)
#endif
#include <iostream>
#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
#pragma warning(pop)
#endif
#include <boost/detail/lightweight_test.hpp>
int f( int x )
{
return x + x;
}
int g( int x )
{
return 2 * x;
}
int main()
{
int x = 4;
int y = x + x;
// bind op value
BOOST_TEST( ( boost::bind( f, _1 ) == y )( x ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) != y )( x ) ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) < y )( x ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) < y + 1 )( x ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) > y )( x ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) > y - 1 )( x ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) <= y - 1 )( x ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) <= y )( x ) );
BOOST_TEST( ( boost::bind( f, _1 ) <= y + 1 )( x ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) >= y + 1 )( x ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) >= y )( x ) );
BOOST_TEST( ( boost::bind( f, _1 ) >= y - 1 )( x ) );
// bind op ref
BOOST_TEST( ( boost::bind( f, _1 ) == boost::ref( y ) )( x ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) != boost::ref( y ) )( x ) ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) < boost::ref( y ) )( x ) ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) > boost::ref( y ) )( x ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) <= boost::ref( y ) )( x ) );
BOOST_TEST( ( boost::bind( f, _1 ) >= boost::ref( y ) )( x ) );
// bind op placeholder
BOOST_TEST( ( boost::bind( f, _1 ) == _2 )( x, y ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) != _2 )( x, y ) ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) < _2 )( x, y ) ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) > _2 )( x, y ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) <= _2 )( x, y ) );
BOOST_TEST( ( boost::bind( f, _1 ) >= _2 )( x, y ) );
// bind op bind
// important: bind( f, _1 ) and bind( g, _1 ) have the same type
BOOST_TEST( ( boost::bind( f, _1 ) == boost::bind( g, _1 ) )( x ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) != boost::bind( g, _1 ) )( x ) ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) < boost::bind( g, _1 ) )( x ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) <= boost::bind( g, _1 ) )( x ) );
BOOST_TEST( !( ( boost::bind( f, _1 ) > boost::bind( g, _1 ) )( x ) ) );
BOOST_TEST( ( boost::bind( f, _1 ) >= boost::bind( g, _1 ) )( x ) );
return boost::report_errors();
}