diff --git a/bind.html b/bind.html index 36ea678..74110c5 100644 --- a/bind.html +++ b/bind.html @@ -60,6 +60,7 @@

Inappropriate use of bind<R>(f, ...)

Binding a nonstandard function

+

Binding an overloaded function

const in signatures

MSVC specific: using boost::bind;

@@ -188,6 +189,27 @@ bind(std::less<int>(), _1, 9)(x); // x < 9

[Note: the ability to omit the return type is not available on all compilers.]

+

By default, bind makes a copy of the provided function object. + boost::ref and boost::cref can be used to make it store + a reference to the function object, rather than a copy. This can be useful when + the function object is noncopyable, expensive to copy, or contains state; of + course, in this case the programmer is expected to ensure that the function + object is not destroyed while it's still being used.

+
struct F2
+{
+    int s;
+
+    typedef void result_type;
+    void operator()( int x ) { s += x; }
+};
+
+F2 f2 = { 0 };
+int a[] = { 1, 2, 3 };
+
+std::for_each( a, a+3, bind( ref(f2), _1 ) );
+
+assert( f2.s == 6 );
+

Using bind with pointers to members

Pointers to member functions and pointers to data members are not function objects, because they do not support operator(). For convenience, bind @@ -285,21 +307,23 @@ std::for_each(v.begin(), v.end(), bind(apply<void>(), _1, 5)); evaluation, use protect(bind(f, ...)).

Overloaded operators (new in Boost 1.33)

For convenience, the function objects produced by bind overload the - logical not operator ! and the relational operators ==, - !=, <, <=, >, - >=.

+ logical not operator ! and the relational and logical operators ==, + !=, <, <=, >, >=, + &&, ||.

!bind(f, ...) is equivalent to bind( logical_not(), bind(f, ...) ), where logical_not is a function object that takes one argument x and returns !x.

-

bind(f, ...) op x, where op is a relational operator, - is equivalent to bind( relation(), bind(f, ...), x ), where relation - is a function object that takes two arguments a and b and - returns a op b.

+

bind(f, ...) op x, where op is a relational or + logical operator, is equivalent to bind( relation(), bind(f, ...), x ), + where relation is a function object that takes two arguments a + and b and returns a op b.

What this means in practice is that you can conveniently negate the result of bind:

std::remove_if( first, last, !bind( &X::visible, _1 ) ); // remove invisible objects

and compare the result of bind against a value:

-

std::find_if( first, last, bind( &X::name, _1 ) == "peter" );

+

std::find_if( first, last, bind( &X::name, _1 ) == "Peter" );

+

std::find_if( first, last, bind( &X::name, _1 ) == "Peter" || bind( + &X::name, _1 ) == "Paul" );

against a placeholder:

bind( &X::name, _1 ) == _2

or against another bind expression:

@@ -362,10 +386,12 @@ void connect() }

Limitations

-

The function objects generated by bind take their arguments by reference - and cannot, therefore, accept non-const temporaries or literal constants. This - is an inherent limitation of the C++ language, known as - the forwarding problem.

+

As a general rule, the function objects generated by bind take their + arguments by reference and cannot, therefore, accept non-const temporaries or + literal constants. This is an inherent limitation of the C++ language in its + current (2003) incarnation, known as + the forwarding problem. (It will be fixed in the next standard, usually + called C++0x.)

The library uses signatures of the form

template<class T> void f(T & t);
@@ -373,17 +399,17 @@ void connect()
 		

to accept arguments of arbitrary types and pass them on unmodified. As noted, this does not work with non-const r-values.

-

An oft-proposed "solution" to this problem is to add an overload: +

On compilers that support partial ordering of function templates, a possible + solution is to add an overload:

template<class T> void f(T & t);
 template<class T> void f(T const & t);
 
-

Unfortunately, this (a) requires providing 512 overloads for nine arguments and - (b) does not actually work for const arguments, both l- and r-values, since the - two templates produce the exact same signature and cannot be partially ordered. -

-

[Note: this is a dark corner of the language, and the - corresponding issue has only recently been resolved.] +

Unfortunately, this requires providing 512 overloads for nine arguments, which + is impractical. The library chooses a small subset: for up to two arguments, it + provides the const overloads in full, for arities of three and more it provides + a single additional overload with all of the arguments taken by const + reference. This covers a reasonable portion of the use cases.

Frequently Asked Questions

Why doesn't this compile?

@@ -528,6 +554,37 @@ int main() recognized by the short form of bind.

See also "__stdcall" and "pascal" Support.

+

Binding an overloaded function

+

An attempt to bind an overloaded function usually results in an error, as there + is no way to tell which overload was meant to be bound. This is a common + problem with member functions with two overloads, const and non-const, as in + this simplified example:

+
struct X
+{
+    int& get();
+    int const& get() const;
+};
+
+int main()
+{
+    boost::bind( &X::get, _1 );
+}
+
+

The ambiguity can be resolved manually by casting the (member) function pointer + to the desired type:

+
int main()
+{
+    boost::bind( static_cast< int const& (X::*) () const >( &X::get ), _1 );
+}
+
+

Another, arguably more readable, alternative is to introduce a temporary + variable:

+
int main()
+{
+    int const& (X::*get) () const = &X::get;
+    boost::bind( get, _1 );
+}
+

const in signatures

Some compilers, including MSVC 6.0 and Borland C++ 5.5.1, have problems with the top-level const in function signatures: @@ -859,7 +916,7 @@ namespace by Jaakko Järvi;

  • The Lambda Library - (now part of Boost) by Jaakko Järvi and Gary Powell (the successor to the + (now part of Boost) by Jaakko Järvi and Gary Powell (the successor to the Binder Library);
  • Extensions to the STL by Petter @@ -890,7 +947,7 @@ namespace

    Copyright © 2001, 2002 by Peter Dimov and Multi Media Ltd. Copyright - 2003-2005 Peter Dimov. Distributed under the Boost Software License, Version + 2003-2008 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.

    diff --git a/include/boost/bind.hpp b/include/boost/bind.hpp index e1076e0..07b84d3 100644 --- a/include/boost/bind.hpp +++ b/include/boost/bind.hpp @@ -248,6 +248,9 @@ public: } }; +struct logical_and; +struct logical_or; + template< class A1, class A2 > class list2: private storage2< A1, A2 > { private: @@ -294,6 +297,26 @@ public: unwrapper::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]); } + template bool operator()( type, logical_and & /*f*/, A & a, int ) + { + return a[ base_type::a1_ ] && a[ base_type::a2_ ]; + } + + template bool operator()( type, logical_and const & /*f*/, A & a, int ) const + { + return a[ base_type::a1_ ] && a[ base_type::a2_ ]; + } + + template bool operator()( type, logical_or & /*f*/, A & a, int ) + { + return a[ base_type::a1_ ] || a[ base_type::a2_ ]; + } + + template bool operator()( type, logical_or const & /*f*/, A & a, int ) const + { + return a[ base_type::a1_ ] || a[ base_type::a2_ ]; + } + template void accept(V & v) const { base_type::accept(v); @@ -1158,6 +1181,9 @@ BOOST_BIND_OPERATOR( <=, less_equal ) BOOST_BIND_OPERATOR( >, greater ) BOOST_BIND_OPERATOR( >=, greater_equal ) +BOOST_BIND_OPERATOR( &&, logical_and ) +BOOST_BIND_OPERATOR( ||, logical_or ) + #undef BOOST_BIND_OPERATOR #if defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) @@ -1542,6 +1568,7 @@ template +#include #undef BOOST_BIND_MF_NAME #undef BOOST_BIND_MF_CC @@ -1552,6 +1579,7 @@ template +#include #undef BOOST_BIND_MF_NAME #undef BOOST_BIND_MF_CC @@ -1564,6 +1592,7 @@ template +#include #undef BOOST_BIND_MF_NAME #undef BOOST_BIND_MF_CC @@ -1576,6 +1605,7 @@ template +#include #undef BOOST_BIND_MF_NAME #undef BOOST_BIND_MF_CC diff --git a/include/boost/bind/bind_mf2_cc.hpp b/include/boost/bind/bind_mf2_cc.hpp new file mode 100644 index 0000000..fdb4495 --- /dev/null +++ b/include/boost/bind/bind_mf2_cc.hpp @@ -0,0 +1,228 @@ +// +// bind/bind_mf2_cc.hpp - member functions, type<> syntax +// +// Do not include this header directly. +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// Copyright (c) 2008 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 +// +// See http://www.boost.org/libs/bind/bind.html for documentation. +// + +// 0 + +template + _bi::bind_t, typename _bi::list_av_1::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (), A1 a1) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf0) F; + typedef typename _bi::list_av_1::type list_type; + return _bi::bind_t(F(f), list_type(a1)); +} + +template + _bi::bind_t, typename _bi::list_av_1::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) () const, A1 a1) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf0) F; + typedef typename _bi::list_av_1::type list_type; + return _bi::bind_t(F(f), list_type(a1)); +} + +// 1 + +template + _bi::bind_t, typename _bi::list_av_2::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1), A1 a1, A2 a2) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf1) F; + typedef typename _bi::list_av_2::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2)); +} + +template + _bi::bind_t, typename _bi::list_av_2::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1) const, A1 a1, A2 a2) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf1) F; + typedef typename _bi::list_av_2::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2)); +} + +// 2 + +template + _bi::bind_t, typename _bi::list_av_3::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2), A1 a1, A2 a2, A3 a3) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf2) F; + typedef typename _bi::list_av_3::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3)); +} + +template + _bi::bind_t, typename _bi::list_av_3::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2) const, A1 a1, A2 a2, A3 a3) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf2) F; + typedef typename _bi::list_av_3::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3)); +} + +// 3 + +template + _bi::bind_t, typename _bi::list_av_4::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3), A1 a1, A2 a2, A3 a3, A4 a4) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf3) F; + typedef typename _bi::list_av_4::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4)); +} + +template + _bi::bind_t, typename _bi::list_av_4::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3) const, A1 a1, A2 a2, A3 a3, A4 a4) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf3) F; + typedef typename _bi::list_av_4::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4)); +} + +// 4 + +template + _bi::bind_t, typename _bi::list_av_5::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf4) F; + typedef typename _bi::list_av_5::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5)); +} + +template + _bi::bind_t, typename _bi::list_av_5::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf4) F; + typedef typename _bi::list_av_5::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5)); +} + +// 5 + +template + _bi::bind_t, typename _bi::list_av_6::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf5) F; + typedef typename _bi::list_av_6::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6)); +} + +template + _bi::bind_t, typename _bi::list_av_6::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf5) F; + typedef typename _bi::list_av_6::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6)); +} + +// 6 + +template + _bi::bind_t, typename _bi::list_av_7::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5, B6), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf6) F; + typedef typename _bi::list_av_7::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7)); +} + +template + _bi::bind_t, typename _bi::list_av_7::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5, B6) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf6) F; + typedef typename _bi::list_av_7::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7)); +} + +// 7 + +template + _bi::bind_t, typename _bi::list_av_8::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5, B6, B7), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf7) F; + typedef typename _bi::list_av_8::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8)); +} + +template + _bi::bind_t, typename _bi::list_av_8::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5, B6, B7) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf7) F; + typedef typename _bi::list_av_8::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8)); +} + +// 8 + +template + _bi::bind_t, typename _bi::list_av_9::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5, B6, B7, B8), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) +{ + typedef _mfi::BOOST_BIND_MF_NAME(mf8) F; + typedef typename _bi::list_av_9::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8, a9)); +} + +template + _bi::bind_t, typename _bi::list_av_9::type> + BOOST_BIND(boost::type, R (BOOST_BIND_MF_CC T::*f) (B1, B2, B3, B4, B5, B6, B7, B8) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) +{ + typedef _mfi::BOOST_BIND_MF_NAME(cmf8) F; + typedef typename _bi::list_av_9::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8, a9)); +} diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index b88d49e..38fb264 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,10 +27,15 @@ test-suite "bind" [ run bind_visit_test.cpp ] [ run bind_placeholder_test.cpp ] [ run bind_rvalue_test.cpp ] + [ run bind_and_or_test.cpp ] [ run mem_fn_test.cpp ] [ run mem_fn_void_test.cpp ] [ run mem_fn_derived_test.cpp ] [ run mem_fn_eq_test.cpp ] [ run mem_fn_dm_test.cpp ] [ run mem_fn_rv_test.cpp ] + [ run ref_fn_test.cpp ] + [ run bind_fnobj2_test.cpp ] + [ run bind_fn2_test.cpp ] + [ run bind_mf2_test.cpp ] ; diff --git a/test/bind_and_or_test.cpp b/test/bind_and_or_test.cpp new file mode 100644 index 0000000..337a11e --- /dev/null +++ b/test/bind_and_or_test.cpp @@ -0,0 +1,84 @@ +#include + +#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_and_or_test.cpp - &&, || operators +// +// Copyright (c) 2008 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 + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(push, 3) +#endif + +#include + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(pop) +#endif + +#include + +bool f( bool x ) +{ + return x; +} + +bool g( bool x ) +{ + return !x; +} + +bool h() +{ + BOOST_ERROR( "Short-circuit evaluation failure" ); + return false; +} + +template< class F, class A1, class A2, class R > void test( F f, A1 a1, A2 a2, R r ) +{ + BOOST_TEST( f( a1, a2 ) == r ); +} + +int main() +{ + // && + + test( boost::bind( f, true ) && boost::bind( g, true ), false, false, f( true ) && g( true ) ); + test( boost::bind( f, true ) && boost::bind( g, false ), false, false, f( true ) && g( false ) ); + + test( boost::bind( f, false ) && boost::bind( h ), false, false, f( false ) && h() ); + + test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, true, f( true ) && g( true ) ); + test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, false, f( true ) && g( false ) ); + + test( boost::bind( f, _1 ) && boost::bind( h ), false, false, f( false ) && h() ); + + // || + + test( boost::bind( f, false ) || boost::bind( g, true ), false, false, f( false ) || g( true ) ); + test( boost::bind( f, false ) || boost::bind( g, false ), false, false, f( false ) || g( false ) ); + + test( boost::bind( f, true ) || boost::bind( h ), false, false, f( true ) || h() ); + + test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, true, f( false ) || g( true ) ); + test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, false, f( false ) || g( false ) ); + + test( boost::bind( f, _1 ) || boost::bind( h ), true, false, f( true ) || h() ); + + // + + return boost::report_errors(); +} diff --git a/test/bind_fn2_test.cpp b/test/bind_fn2_test.cpp new file mode 100644 index 0000000..93f587c --- /dev/null +++ b/test/bind_fn2_test.cpp @@ -0,0 +1,171 @@ +#include + +#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_fn2_test.cpp - test for functions w/ the type<> syntax +// +// Copyright (c) 2005, 2008 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 + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(push, 3) +#endif + +#include + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(pop) +#endif + +#include + +long global_result; + +// long + +long f_0() +{ + return global_result = 17041L; +} + +long f_1(long a) +{ + return global_result = a; +} + +long f_2(long a, long b) +{ + return global_result = a + 10 * b; +} + +long f_3(long a, long b, long c) +{ + return global_result = a + 10 * b + 100 * c; +} + +long f_4(long a, long b, long c, long d) +{ + return global_result = a + 10 * b + 100 * c + 1000 * d; +} + +long f_5(long a, long b, long c, long d, long e) +{ + return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e; +} + +long f_6(long a, long b, long c, long d, long e, long f) +{ + return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f; +} + +long f_7(long a, long b, long c, long d, long e, long f, long g) +{ + return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g; +} + +long f_8(long a, long b, long c, long d, long e, long f, long g, long h) +{ + return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h; +} + +long f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i) +{ + return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i; +} + +// void + +void fv_0() +{ + global_result = 17041L; +} + +void fv_1(long a) +{ + global_result = a; +} + +void fv_2(long a, long b) +{ + global_result = a + 10 * b; +} + +void fv_3(long a, long b, long c) +{ + global_result = a + 10 * b + 100 * c; +} + +void fv_4(long a, long b, long c, long d) +{ + global_result = a + 10 * b + 100 * c + 1000 * d; +} + +void fv_5(long a, long b, long c, long d, long e) +{ + global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e; +} + +void fv_6(long a, long b, long c, long d, long e, long f) +{ + global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f; +} + +void fv_7(long a, long b, long c, long d, long e, long f, long g) +{ + global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g; +} + +void fv_8(long a, long b, long c, long d, long e, long f, long g, long h) +{ + global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h; +} + +void fv_9(long a, long b, long c, long d, long e, long f, long g, long h, long i) +{ + global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i; +} + +void function_test() +{ + using namespace boost; + + bind( type(), f_0 )(); BOOST_TEST( global_result == 17041L ); + bind( type(), f_1, 1 )(); BOOST_TEST( global_result == 1L ); + bind( type(), f_2, 1, 2 )(); BOOST_TEST( global_result == 21L ); + bind( type(), f_3, 1, 2, 3 )(); BOOST_TEST( global_result == 321L ); + bind( type(), f_4, 1, 2, 3, 4 )(); BOOST_TEST( global_result == 4321L ); + bind( type(), f_5, 1, 2, 3, 4, 5 )(); BOOST_TEST( global_result == 54321L ); + bind( type(), f_6, 1, 2, 3, 4, 5, 6 )(); BOOST_TEST( global_result == 654321L ); + bind( type(), f_7, 1, 2, 3, 4, 5, 6, 7 )(); BOOST_TEST( global_result == 7654321L ); + bind( type(), f_8, 1, 2, 3, 4, 5, 6, 7, 8 )(); BOOST_TEST( global_result == 87654321L ); + bind( type(), f_9, 1, 2, 3, 4, 5, 6, 7, 8, 9 )(); BOOST_TEST( global_result == 987654321L ); + + bind( type(), fv_0 )(); BOOST_TEST( global_result == 17041L ); + bind( type(), fv_1, 1 )(); BOOST_TEST( global_result == 1L ); + bind( type(), fv_2, 1, 2 )(); BOOST_TEST( global_result == 21L ); + bind( type(), fv_3, 1, 2, 3 )(); BOOST_TEST( global_result == 321L ); + bind( type(), fv_4, 1, 2, 3, 4 )(); BOOST_TEST( global_result == 4321L ); + bind( type(), fv_5, 1, 2, 3, 4, 5 )(); BOOST_TEST( global_result == 54321L ); + bind( type(), fv_6, 1, 2, 3, 4, 5, 6 )(); BOOST_TEST( global_result == 654321L ); + bind( type(), fv_7, 1, 2, 3, 4, 5, 6, 7 )(); BOOST_TEST( global_result == 7654321L ); + bind( type(), fv_8, 1, 2, 3, 4, 5, 6, 7, 8 )(); BOOST_TEST( global_result == 87654321L ); + bind( type(), fv_9, 1, 2, 3, 4, 5, 6, 7, 8, 9 )(); BOOST_TEST( global_result == 987654321L ); +} + +int main() +{ + function_test(); + return boost::report_errors(); +} diff --git a/test/bind_fnobj2_test.cpp b/test/bind_fnobj2_test.cpp new file mode 100644 index 0000000..a85fe5d --- /dev/null +++ b/test/bind_fnobj2_test.cpp @@ -0,0 +1,76 @@ +#include + +#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_fnobj2_test.cpp - test for function objects w/ the type<> syntax +// +// Copyright (c) 2005, 2008 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 + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(push, 3) +#endif + +#include + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(pop) +#endif + +#include + +struct X +{ + mutable unsigned int hash; + + X(): hash(0) {} + + int operator()() const { operator()(17); return 0; } + int operator()(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; } + int operator()(int a1, int a2) const { operator()(a1); operator()(a2); return 0; } + int operator()(int a1, int a2, int a3) const { operator()(a1, a2); operator()(a3); return 0; } + int operator()(int a1, int a2, int a3, int a4) const { operator()(a1, a2, a3); operator()(a4); return 0; } + int operator()(int a1, int a2, int a3, int a4, int a5) const { operator()(a1, a2, a3, a4); operator()(a5); return 0; } + int operator()(int a1, int a2, int a3, int a4, int a5, int a6) const { operator()(a1, a2, a3, a4, a5); operator()(a6); return 0; } + int operator()(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { operator()(a1, a2, a3, a4, a5, a6); operator()(a7); return 0; } + int operator()(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { operator()(a1, a2, a3, a4, a5, a6, a7); operator()(a8); return 0; } + int operator()(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9) const { operator()(a1, a2, a3, a4, a5, a6, a7, a8); operator()(a9); return 0; } +}; + +void function_object_test() +{ + using namespace boost; + + X x; + + bind( type(), ref(x) )(); + bind( type(), ref(x), 1 )(); + bind( type(), ref(x), 1, 2 )(); + bind( type(), ref(x), 1, 2, 3 )(); + bind( type(), ref(x), 1, 2, 3, 4 )(); + bind( type(), ref(x), 1, 2, 3, 4, 5 )(); + bind( type(), ref(x), 1, 2, 3, 4, 5, 6 )(); + bind( type(), ref(x), 1, 2, 3, 4, 5, 6, 7)(); + bind( type(), ref(x), 1, 2, 3, 4, 5, 6, 7, 8 )(); + bind( type(), ref(x), 1, 2, 3, 4, 5, 6, 7, 8, 9 )(); + + BOOST_TEST( x.hash == 9932 ); +} + +int main() +{ + function_object_test(); + return boost::report_errors(); +} diff --git a/test/bind_lookup_problem_test.cpp b/test/bind_lookup_problem_test.cpp index 3ed4178..269b80a 100644 --- a/test/bind_lookup_problem_test.cpp +++ b/test/bind_lookup_problem_test.cpp @@ -1,7 +1,7 @@ // // bind_lookup_problem_test.cpp // -// Copyright (C) Markus Schöpflin 2005. +// Copyright (C) Markus Schoepflin 2005. // // Use, modification, and distribution are subject to the Boost Software // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at diff --git a/test/bind_mf2_test.cpp b/test/bind_mf2_test.cpp new file mode 100644 index 0000000..c04f958 --- /dev/null +++ b/test/bind_mf2_test.cpp @@ -0,0 +1,162 @@ +#include + +#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_mf2_test.cpp - test for member functions w/ the type<> syntax +// +// Copyright (c) 2005, 2008 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 + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(push, 3) +#endif + +#include + +#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300) +#pragma warning(pop) +#endif + +#include + +struct X +{ + mutable unsigned int hash; + + X(): hash(0) {} + + int f0() { f1(17); return 0; } + int g0() const { g1(17); return 0; } + + int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; } + int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; } + + int f2(int a1, int a2) { f1(a1); f1(a2); return 0; } + int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; } + + int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; } + int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; } + + int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; } + int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; } + + int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; } + int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; } + + int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; } + int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); return 0; } + + int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; } + int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); return 0; } + + int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); return 0; } + int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); return 0; } +}; + +void member_function_test() +{ + using namespace boost; + + X x; + + // 0 + + bind( type(), &X::f0, &x )(); + bind( type(), &X::f0, ref(x) )(); + + bind( type(), &X::g0, &x )(); + bind( type(), &X::g0, x )(); + bind( type(), &X::g0, ref(x) )(); + + // 1 + + bind( type(), &X::f1, &x, 1 )(); + bind( type(), &X::f1, ref(x), 1 )(); + + bind( type(), &X::g1, &x, 1 )(); + bind( type(), &X::g1, x, 1 )(); + bind( type(), &X::g1, ref(x), 1 )(); + + // 2 + + bind( type(), &X::f2, &x, 1, 2 )(); + bind( type(), &X::f2, ref(x), 1, 2 )(); + + bind( type(), &X::g2, &x, 1, 2 )(); + bind( type(), &X::g2, x, 1, 2 )(); + bind( type(), &X::g2, ref(x), 1, 2 )(); + + // 3 + + bind( type(), &X::f3, &x, 1, 2, 3 )(); + bind( type(), &X::f3, ref(x), 1, 2, 3 )(); + + bind( type(), &X::g3, &x, 1, 2, 3 )(); + bind( type(), &X::g3, x, 1, 2, 3 )(); + bind( type(), &X::g3, ref(x), 1, 2, 3 )(); + + // 4 + + bind( type(), &X::f4, &x, 1, 2, 3, 4 )(); + bind( type(), &X::f4, ref(x), 1, 2, 3, 4 )(); + + bind( type(), &X::g4, &x, 1, 2, 3, 4 )(); + bind( type(), &X::g4, x, 1, 2, 3, 4 )(); + bind( type(), &X::g4, ref(x), 1, 2, 3, 4 )(); + + // 5 + + bind( type(), &X::f5, &x, 1, 2, 3, 4, 5 )(); + bind( type(), &X::f5, ref(x), 1, 2, 3, 4, 5 )(); + + bind( type(), &X::g5, &x, 1, 2, 3, 4, 5 )(); + bind( type(), &X::g5, x, 1, 2, 3, 4, 5 )(); + bind( type(), &X::g5, ref(x), 1, 2, 3, 4, 5 )(); + + // 6 + + bind( type(), &X::f6, &x, 1, 2, 3, 4, 5, 6 )(); + bind( type(), &X::f6, ref(x), 1, 2, 3, 4, 5, 6 )(); + + bind( type(), &X::g6, &x, 1, 2, 3, 4, 5, 6 )(); + bind( type(), &X::g6, x, 1, 2, 3, 4, 5, 6 )(); + bind( type(), &X::g6, ref(x), 1, 2, 3, 4, 5, 6 )(); + + // 7 + + bind( type(), &X::f7, &x, 1, 2, 3, 4, 5, 6, 7)(); + bind( type(), &X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); + + bind( type(), &X::g7, &x, 1, 2, 3, 4, 5, 6, 7)(); + bind( type(), &X::g7, x, 1, 2, 3, 4, 5, 6, 7)(); + bind( type(), &X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); + + // 8 + + bind( type(), &X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8 )(); + bind( type(), &X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8 )(); + + bind( type(), &X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8 )(); + bind( type(), &X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8 )(); + bind( type(), &X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8 )(); + + BOOST_TEST( x.hash == 23558 ); +} + +int main() +{ + member_function_test(); + return boost::report_errors(); +} diff --git a/test/ref_fn_test.cpp b/test/ref_fn_test.cpp new file mode 100644 index 0000000..aec54e8 --- /dev/null +++ b/test/ref_fn_test.cpp @@ -0,0 +1,81 @@ +#include + +#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 + +// ref_fn_test.cpp: ref( f ) +// +// Copyright (c) 2008 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 + + +void f0() +{ +} + +void f1(int) +{ +} + +void f2(int, int) +{ +} + +void f3(int, int, int) +{ +} + +void f4(int, int, int, int) +{ +} + +void f5(int, int, int, int, int) +{ +} + +void f6(int, int, int, int, int, int) +{ +} + +void f7(int, int, int, int, int, int, int) +{ +} + +void f8(int, int, int, int, int, int, int, int) +{ +} + +void f9(int, int, int, int, int, int, int, int, int) +{ +} + +#define BOOST_TEST_REF( f ) BOOST_TEST( &boost::ref( f ).get() == &f ) + +int main() +{ + int v = 0; + BOOST_TEST_REF( v ); + + BOOST_TEST_REF( f0 ); + BOOST_TEST_REF( f1 ); + BOOST_TEST_REF( f2 ); + BOOST_TEST_REF( f3 ); + BOOST_TEST_REF( f4 ); + BOOST_TEST_REF( f5 ); + BOOST_TEST_REF( f6 ); + BOOST_TEST_REF( f7 ); + BOOST_TEST_REF( f8 ); + BOOST_TEST_REF( f9 ); + + return boost::report_errors(); +}