### Nested Bind Example 1 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates nested bind calls with placeholders. ```C++ bind(f_1, bind(f_1, _1))(x); bind(f_1, bind(f_2, _1, _2))(x, y); bind(f_2, bind(f_1, _1), bind(f_1, _1))(x); bind(f_2, bind(f_1, _1), bind(f_1, _2))(x, y); bind(f_1, bind(f_0))(); ``` -------------------------------- ### Test Setup and Structure Definition Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_stdcall_mf_test.cpp Defines the necessary preprocessor directives and the `X` struct containing member functions with the `__stdcall` calling convention. This setup is required for the subsequent binding tests. ```cpp #define BOOST_MEM_FN_ENABLE_STDCALL #include #include struct X { mutable unsigned int hash; X(): hash(0) {} int __stdcall f0() { f1(17); return 0; } int __stdcall g0() const { g1(17); return 0; } int __stdcall f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; } int __stdcall g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; } int __stdcall f2(int a1, int a2) { f1(a1); f1(a2); return 0; } int __stdcall g2(int a1, int a2) const { g1(a1); g1(a2); return 0; } int __stdcall f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; } int __stdcall g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; } int __stdcall f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; } int __stdcall g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; } int __stdcall f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; } int __stdcall g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; } int __stdcall f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; } int __stdcall 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 __stdcall 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 __stdcall 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 __stdcall 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 __stdcall 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; } }; ``` -------------------------------- ### Test Setup for Boost Bind Fastcall Member Functions Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_mf_test.cpp Includes necessary headers and defines BOOST_MEM_FN_ENABLE_FASTCALL for testing __fastcall member functions with Boost.Bind. This setup is specific to MSVC environments. ```cpp #define BOOST_MEM_FN_ENABLE_FASTCALL #include #include ``` -------------------------------- ### Nested Bind Example 2 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates nested bind calls with placeholders and void functions. ```C++ (bind(fv_1, bind(f_1, _1))(x), (global_result == 1L)); (bind(fv_1, bind(f_2, _1, _2))(x, y), (global_result == 21L)); (bind(fv_2, bind(f_1, _1), bind(f_1, _1))(x), (global_result == 11L)); (bind(fv_2, bind(f_1, _1), bind(f_1, _2))(x, y), (global_result == 21L)); (bind(fv_1, bind(f_0))(), (global_result == 17041L)); ``` -------------------------------- ### Define Sample Functions Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Defines two sample functions, f and g, for use in bind examples. ```cpp int f(int a, int b) { return a + b; } int g(int a, int b, int c) { return a + b + c; } ``` -------------------------------- ### Using Bind with Standard Algorithms (Animation Example) Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Shows how to use Boost.Bind with standard algorithms like std::for_each and std::remove_if to manage a collection of animation objects. It demonstrates binding member functions and using helper functions. ```cpp class image; class animation { public: void advance(int ms); bool inactive() const; void render(image & target) const; }; std::vector anims; template void erase_if(C & c, P pred) { c.erase(std::remove_if(c.begin(), c.end(), pred), c.end()); } void update(int ms) { std::for_each(anims.begin(), anims.end(), boost::bind(&animation::advance, _1, ms)); erase_if(anims, boost::mem_fn(&animation::inactive)); } void render(image & target) { std::for_each(anims.begin(), anims.end(), boost::bind(&animation::render, _1, boost::ref(target))); } ``` -------------------------------- ### Binding Member Functions with 6 Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_mf_test.cpp Demonstrates binding member functions 'f6' and 'g6' of class 'X' with six arguments. Includes examples using a pointer to member and `boost::ref`, passing six integer arguments. ```cpp // 6 bind(&X::f6, &x, 1, 2, 3, 4, 5, 6)(); bind(&X::f6, ref(x), 1, 2, 3, 4, 5, 6)(); bind(&X::g6, &x, 1, 2, 3, 4, 5, 6)(); bind(&X::g6, x, 1, 2, 3, 4, 5, 6)(); bind(&X::g6, ref(x), 1, 2, 3, 4, 5, 6)(); ``` -------------------------------- ### Binding Member Functions with 3 Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_mf_test.cpp Demonstrates binding member functions 'f3' and 'g3' of class 'X' with three arguments. Includes examples using a pointer to member and `boost::ref`, passing three integer arguments. ```cpp // 3 bind(&X::f3, &x, 1, 2, 3)(); bind(&X::f3, ref(x), 1, 2, 3)(); bind(&X::g3, &x, 1, 2, 3)(); bind(&X::g3, x, 1, 2, 3)(); bind(&X::g3, ref(x), 1, 2, 3)(); ``` -------------------------------- ### Calling member function and checking return value Source: https://www.boost.org/doc/libs/latest/libs/bind/test/mem_fn_test.cpp This example demonstrates calling a member function that returns a value and using that value in a comparison. It also shows calling another member function with different argument types. ```cpp return detect_errors(mem_fn(&X::hash)(x) == 17610 && mem_fn(&X::hash)(sp) == 2155); ``` -------------------------------- ### Library-defined for_each with mem_fn Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/mem_fn.html An example of a library defining an enhanced for_each algorithm that utilizes boost::mem_fn to simplify member function invocation. ```cpp template void for_each(It first, It last, R (T::*pmf) ()) { std::for_each(first, last, boost::mem_fn(pmf)); } ``` -------------------------------- ### Composing f with h(x, y) Source: https://www.boost.org/doc/libs/latest/libs/bind/bind_as_compose.cpp This example demonstrates composing function `f` with the result of function `h` applied to its first two arguments (`_1`, `_2`). This is useful when `f` needs to receive the combined output of `h` which itself takes two arguments. ```C++ #include #include #include using namespace boost::placeholders; std::string f(std::string const & x) { return "f(" + x + ")"; } std::string h(std::string const & x, std::string const & y) { return "h(" + x + ", " + y + ")"; } template void test(F f) { std::cout << f("x", "y") << '\n'; } int main() { using namespace boost; // compose_f_hxy test( bind(f, bind(h, _1, _2)) ); return 0; } ``` -------------------------------- ### Test mem_fn with X::f5 and X::g5 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/mem_fn_fastcall_test.cpp Demonstrates the use of boost::mem_fn with X::f5 and X::g5, which take five integer arguments. The code includes examples of calling these functions with object instances passed by value, pointer, and shared_ptr, along with a const reference. ```cpp mem_fn(&X::f5)(x, 1, 2, 3, 4, 5); mem_fn(&X::f5)(&x, 1, 2, 3, 4, 5); mem_fn(&X::f5)(sp, 1, 2, 3, 4, 5); mem_fn(&X::g5)(x, 1, 2, 3, 4, 5); mem_fn(&X::g5)(rcx, 1, 2, 3, 4, 5); mem_fn(&X::g5)(&x, 1, 2, 3, 4, 5); mem_fn(&X::g5)(pcx, 1, 2, 3, 4, 5); mem_fn(&X::g5)(sp, 1, 2, 3, 4, 5); ``` -------------------------------- ### Test mem_fn with X::f7 and X::g7 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/mem_fn_fastcall_test.cpp Demonstrates the invocation of X::f7 and X::g7 using boost::mem_fn, with these functions accepting seven integer arguments. The examples cover calls with object instances passed by value, pointer, and shared_ptr, including a const reference. ```cpp mem_fn(&X::f7)(x, 1, 2, 3, 4, 5, 6, 7); mem_fn(&X::f7)(&x, 1, 2, 3, 4, 5, 6, 7); mem_fn(&X::f7)(sp, 1, 2, 3, 4, 5, 6, 7); mem_fn(&X::g7)(x, 1, 2, 3, 4, 5, 6, 7); mem_fn(&X::g7)(rcx, 1, 2, 3, 4, 5, 6, 7); mem_fn(&X::g7)(&x, 1, 2, 3, 4, 5, 6, 7); mem_fn(&X::g7)(pcx, 1, 2, 3, 4, 5, 6, 7); ``` -------------------------------- ### Test mem_fn with X::f2 and X::g2 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/mem_fn_fastcall_test.cpp Demonstrates using boost::mem_fn to call X::f2 and X::g2, which accept two integer arguments. The examples show calls with various object instance types (value, pointer, shared_ptr) and a const reference. ```cpp mem_fn(&X::f2)(x, 1, 2); mem_fn(&X::f2)(&x, 1, 2); mem_fn(&X::f2)(sp, 1, 2); mem_fn(&X::g2)(x, 1, 2); mem_fn(&X::g2)(rcx, 1, 2); mem_fn(&X::g2)(&x, 1, 2); mem_fn(&X::g2)(pcx, 1, 2); mem_fn(&X::g2)(sp, 1, 2); ``` -------------------------------- ### Using Bind with Boost.Function (Button Example) Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Illustrates how Boost.Bind can be used to assign member functions to Boost.Function objects, commonly used for event handling like button clicks. It shows binding member functions of a player object to button onClick events. ```cpp class button { public: boost::function onClick; }; class player { public: void play(); void stop(); }; button playButton, stopButton; player thePlayer; void connect() { playButton.onClick = boost::bind(&player::play, &thePlayer); stopButton.onClick = boost::bind(&player::stop, &thePlayer); } ``` -------------------------------- ### get_pointer Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/mem_fn.html A utility function to get a pointer to an object. It simply returns the input pointer. ```APIDOC ## get_pointer ### Description Returns `p`. ### Synopsis ```cpp template T * get_pointer(T * p); ``` ### Throws Nothing. ``` -------------------------------- ### Binding Overloaded Function - Ambiguity Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Shows an example where binding an overloaded function leads to ambiguity because the compiler cannot determine which overload to select. This is common with const and non-const member functions. ```C++ struct X { int& get(); int const& get() const; }; int main() { boost::bind(&X::get, _1); } ``` -------------------------------- ### Applying Bind with a Helper Function Object Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Shows how to use a helper function object like 'apply' to correctly apply a bound function object when the first argument needs to be evaluated. ```cpp typedef void (*pf)(int); std::vector v; std::for_each(v.begin(), v.end(), bind(_1, 5)); ``` ```cpp typedef void (*pf)(int); std::vector v; std::for_each(v.begin(), v.end(), bind(apply(), _1, 5)); ``` -------------------------------- ### Incorrect argument count for bind Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Example of a compilation error when the number of arguments provided to boost::bind does not match the function's arity. The error is typically reported at bind time. ```C++ int f(int, int); int main() { boost::bind(f, 1); // error, f takes two arguments boost::bind(f, 1, 2); // OK } ``` -------------------------------- ### Convenient syntax with library-defined for_each Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/mem_fn.html Illustrates the simplified syntax enabled by a library-defined for_each algorithm that internally uses boost::mem_fn. ```cpp for_each(v.begin(), v.end(), &Shape::draw); ``` -------------------------------- ### Bind First Argument with Placeholder Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Demonstrates binding the first argument of f to 5 and using a placeholder for the second. ```cpp bind(f, 5, _1)(x) ``` -------------------------------- ### Enabling pascal for Mac toolbox functions Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Demonstrates enabling support for pascal calling convention, used by Mac toolbox functions, by defining a preprocessor macro. ```C++ #define BOOST_BIND_ENABLE_PASCAL ``` -------------------------------- ### Bind Function Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Demonstrates binding all arguments to a function 'f' and selectively binding arguments using placeholders like '_1'. ```cpp bind(f, 1, 2) bind(g, 1, 2, 3)() ``` -------------------------------- ### Standard Library Equivalent Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Shows the equivalent operation using std::bind2nd and std::ptr_fun. ```cpp std::bind2nd(std::ptr_fun(f), 5)(x) ``` -------------------------------- ### Test mem_fn with X::f4 and X::g4 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/mem_fn_fastcall_test.cpp Shows how boost::mem_fn can be used to invoke X::f4 and X::g4, which accept four integer arguments. The examples cover calls with object instances passed by value, pointer, and shared_ptr, including a const reference. ```cpp mem_fn(&X::f4)(x, 1, 2, 3, 4); mem_fn(&X::f4)(&x, 1, 2, 3, 4); mem_fn(&X::f4)(sp, 1, 2, 3, 4); mem_fn(&X::g4)(x, 1, 2, 3, 4); mem_fn(&X::g4)(rcx, 1, 2, 3, 4); mem_fn(&X::g4)(&x, 1, 2, 3, 4); mem_fn(&X::g4)(pcx, 1, 2, 3, 4); mem_fn(&X::g4)(sp, 1, 2, 3, 4); ``` -------------------------------- ### Composing h with f(x) and g(y) Source: https://www.boost.org/doc/libs/latest/libs/bind/bind_as_compose.cpp This example composes function `h` using the result of `f` applied to `_1` and `g` applied to `_2`. This is suitable when `h` needs two distinct arguments, each processed independently by `f` and `g` respectively, using different placeholders. ```C++ #include #include #include using namespace boost::placeholders; std::string f(std::string const & x) { return "f(" + x + ")"; } std::string g(std::string const & x) { return "g(" + x + ")"; } std::string h(std::string const & x, std::string const & y) { return "h(" + x + ", " + y + ")"; } template void test(F f) { std::cout << f("x", "y") << '\n'; } int main() { using namespace boost; // compose_h_fx_gy test( bind(h, bind(f, _1), bind(g, _2)) ); return 0; } ``` -------------------------------- ### Applying a Function Object with Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Shows how to use a helper function object 'apply' to evaluate the first argument as a function and apply it to the rest of the argument list. This is useful when the bound function object itself needs to be evaluated. ```cpp typedef void (*pf)(int); std::vector v; std::for_each(v.begin(), v.end(), bind(apply(), _1, 5)); ``` -------------------------------- ### Bind with printf (variable arguments) Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Demonstrates using bind with variable-argument functions like printf. The `bind(f, ...)` syntax is used when bind cannot automatically determine the function's signature. ```C++ bind(printf, ...) ``` -------------------------------- ### Using mem_fn with std::for_each and smart pointers Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/mem_fn.html Demonstrates how to use boost::mem_fn with std::for_each when the container stores smart pointers to objects. ```cpp std::for_each(v.begin(), v.end(), boost::mem_fn(&Shape::draw)); ``` -------------------------------- ### Using Overloaded Operators with Bind Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Demonstrates the convenience of overloaded operators like '!' and relational/logical operators when used with bind expressions for common operations. ```cpp std::remove_if(first, last, !bind(&X::visible, _1)); // remove invisible objects ``` ```cpp std::find_if(first, last, bind(&X::name, _1) == "Peter"); ``` ```cpp std::find_if(first, last, bind(&X::name, _1) == "Peter" || bind(&X::name, _1) == "Paul"); ``` ```cpp bind(&X::name, _1) == _2 ``` ```cpp std::sort(first, last, bind(&X::name, _1) < bind(&X::name, _2)); // sort by name ``` -------------------------------- ### Storing References with boost::ref and boost::cref Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Demonstrates how to use boost::ref and boost::cref to store references to arguments instead of copies within the bound function object. ```cpp int i = 5; bind(f, ref(i), _1); bind(f, cref(i), _1); ``` -------------------------------- ### Bind Member Function f7 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a member function 'f7' with different argument passing methods (pointer, ref) and 7 arguments. ```C++ bind(&X::f7, &x, 1, 2, 3, 4, 5, 6, 7)(); bind(&X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); ``` -------------------------------- ### Comparing Bind Expressions with Placeholders Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Illustrates comparing the result of a bind expression with a placeholder argument. This is useful for creating flexible comparison predicates. ```cpp bind(&X::name, _1) == _2 ``` -------------------------------- ### Enabling stdcall for COM methods Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Illustrates enabling support for stdcall calling convention for COM methods by defining a specific preprocessor macro. ```C++ #define BOOST_MEM_FN_ENABLE_STDCALL ``` -------------------------------- ### Bind function with type and placeholder arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a function with explicit return type and a placeholder for a later-supplied argument. ```C++ BOOST_TEST( bind(type(), Y(), i, _1)(k) == 38 ); BOOST_TEST( bind(type(), Y(), i, _1, 9)(k) == 938 ); ``` -------------------------------- ### Bind Member Function f5 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a member function 'f5' with different argument passing methods (pointer, ref) and 5 arguments. ```C++ bind(&X::f5, &x, 1, 2, 3, 4, 5)(); bind(&X::f5, ref(x), 1, 2, 3, 4, 5)(); ``` -------------------------------- ### Bind Some Arguments with Placeholder Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Binds the second argument of f to 5 and uses a placeholder for the first argument. ```cpp bind(f, _1, 5)(x) ``` -------------------------------- ### Binding Member Functions with 7 Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_mf_test.cpp Illustrates binding member functions 'f7' and 'g7' of class 'X' with seven arguments. Shows binding with a pointer to member and `boost::ref`, passing seven integer arguments. ```cpp // 7 bind(&X::f7, &x, 1, 2, 3, 4, 5, 6, 7)(); bind(&X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); bind(&X::g7, &x, 1, 2, 3, 4, 5, 6, 7)(); bind(&X::g7, x, 1, 2, 3, 4, 5, 6, 7)(); bind(&X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); ``` -------------------------------- ### General Argument Substitution with Placeholders Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Shows advanced argument reordering and repetition using placeholders like '_1', '_2', and '_3' with functions taking multiple arguments. ```cpp bind(f, _2, _1)(x, y); bind(g, _1, 9, _1)(x); bind(g, _3, _3, _3)(x, y, z); bind(g, _1, _1, _1)(x, y, z); ``` -------------------------------- ### Bind Member Function f6 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a member function 'f6' with different argument passing methods (pointer, ref) and 6 arguments. ```C++ bind(&X::f6, &x, 1, 2, 3, 4, 5, 6)(); bind(&X::f6, ref(x), 1, 2, 3, 4, 5, 6)(); ``` -------------------------------- ### bind(R (*f) (B1), A1 a1) Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Creates a function object that calls a free function pointer `f` with one argument `B1`, passing `μ(a1, v1, ..., vm)` as that argument. It throws if the copy constructor of `A1` throws. ```APIDOC ## bind(R (*f) (B1), A1 a1) ### Description Creates a function object that calls a free function pointer `f` with one argument `B1`, passing `μ(a1, v1, ..., vm)` as that argument. It throws if the copy constructor of `A1` throws. ### Returns A function object λ such that the expression λ`(v1, v2, ..., vm)` is equivalent to `f(`μ`(a1, v1, v2, ..., vm))`. ### Throws Nothing unless the copy constructor of `A1` throws an exception. ``` -------------------------------- ### bind(R (*f) (B1, B2), A1 a1, A2 a2) Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Creates a function object that calls a free function pointer `f` with two arguments `B1` and `B2`, passing `μ(a1, v1, ..., vm)` and `μ(a2, v1, ..., vm)` as those arguments. It throws if the copy constructors of `A1` or `A2` throw. ```APIDOC ## bind(R (*f) (B1, B2), A1 a1, A2 a2) ### Description Creates a function object that calls a free function pointer `f` with two arguments `B1` and `B2`, passing `μ(a1, v1, ..., vm)` and `μ(a2, v1, ..., vm)` as those arguments. It throws if the copy constructors of `A1` or `A2` throw. ### Returns A function object λ such that the expression λ`(v1, v2, ..., vm)` is equivalent to `f(`μ`(a1, v1, v2, ..., vm), `μ`(a2, v1, v2, ..., vm))`. ### Throws Nothing unless the copy constructors of `A1` or `A2` throw an exception. ``` -------------------------------- ### Bind Void Member Function f7 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a void member function 'f7' with different argument passing methods (pointer, ref) and 7 arguments. ```C++ bind(&V::f7, &v, 1, 2, 3, 4, 5, 6, 7)(); bind(&V::f7, ref(v), 1, 2, 3, 4, 5, 6, 7)(); ``` -------------------------------- ### Binding Member Functions with 0 Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_mf_test.cpp Demonstrates binding member functions 'f0' and 'g0' of class 'X' with zero arguments. Shows usage with both a pointer to member and `boost::ref` for the object. ```cpp X x; // 0 bind(&X::f0, &x)(); bind(&X::f0, ref(x))(); bind(&X::g0, &x)(); bind(&X::g0, x)(); bind(&X::g0, ref(x))(); ``` -------------------------------- ### Bind member function f4 (four arguments) Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding the `f4` member function of class `X` with four arguments. ```C++ bind(&X::f4, &x, 1, 2, 3, 4)(); ``` -------------------------------- ### Bind function object with type and reference arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Illustrates binding a function object using `type<>` for return type specification and `ref()` for passing arguments by reference. ```C++ BOOST_TEST( bind(type(), Y(), ref(i))() == 7 ); BOOST_TEST( bind(type(), Y(), ref(i))() == 8 ); ``` -------------------------------- ### bind(F f, A1 a1, A2 a2) Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Creates a function object that calls `f` with the results of `μ(a1, v1, ..., vm)` and `μ(a2, v1, ..., vm)` as its arguments, implicitly converted to `R`. It throws if the copy constructors of `F`, `A1`, or `A2` throw. ```APIDOC ## bind(F f, A1 a1, A2 a2) ### Description Creates a function object that calls `f` with the results of `μ(a1, v1, ..., vm)` and `μ(a2, v1, ..., vm)` as its arguments, implicitly converted to `R`. It throws if the copy constructors of `F`, `A1`, or `A2` throw. ### Returns A function object λ such that the expression λ`(v1, v2, ..., vm)` is equivalent to `f(`μ`(a1, v1, v2, ..., vm), `μ`(a2, v1, v2, ..., vm))`, implicitly converted to `R`. ### Throws Nothing unless the copy constructors of `F`, `A1` or `A2` throw an exception. ``` -------------------------------- ### Bind Void Member Function f5 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a void member function 'f5' with different argument passing methods (pointer, ref) and 5 arguments. ```C++ bind(&V::f5, &v, 1, 2, 3, 4, 5)(); bind(&V::f5, ref(v), 1, 2, 3, 4, 5)(); ``` -------------------------------- ### Enabling stdcall for Windows API functions Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Shows how to enable support for stdcall calling convention, commonly used by Windows API functions, by defining a preprocessor macro. ```C++ #define BOOST_BIND_ENABLE_STDCALL ``` -------------------------------- ### Bind with Member Functions Source: https://www.boost.org/doc/libs/latest/libs/bind/doc/html/bind.html Demonstrates using boost::bind with pointers to member functions. Boost.Bind treats member pointers as if they were converted by boost::mem_fn. ```C++ struct X { bool f(int a); }; X x; shared_ptr p(new X); int i = 5; bind(&X::f, ref(x), _1)(i); // x.f(i) bind(&X::f, &x, _1)(i); // (&x)->f(i) bind(&X::f, x, _1)(i); // (internal copy of x).f(i) bind(&X::f, p, _1)(i); // (internal copy of p)->f(i) ``` -------------------------------- ### bind(F f, A1 a1) Source: https://www.boost.org/doc/libs/latest/libs/bind/bind.html Creates a function object that calls `f` with the result of `μ(a1, v1, ..., vm)` as its argument, implicitly converted to `R`. It throws if the copy constructors of `F` or `A1` throw. ```APIDOC ## bind(F f, A1 a1) ### Description Creates a function object that calls `f` with the result of `μ(a1, v1, ..., vm)` as its argument, implicitly converted to `R`. It throws if the copy constructors of `F` or `A1` throw. ### Returns A function object λ such that the expression λ`(v1, v2, ..., vm)` is equivalent to `f(`μ`(a1, v1, v2, ..., vm))`, implicitly converted to `R`. ### Throws Nothing unless the copy constructors of `F` or `A1` throw an exception. ``` -------------------------------- ### Bind Void Member Function f6 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a void member function 'f6' with different argument passing methods (pointer, ref) and 6 arguments. ```C++ bind(&V::f6, &v, 1, 2, 3, 4, 5, 6)(); bind(&V::f6, ref(v), 1, 2, 3, 4, 5, 6)(); ``` -------------------------------- ### Bind Member Function f8 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a member function 'f8' with different argument passing methods (pointer, ref) and 8 arguments. ```C++ bind(&X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8)(); bind(&X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)(); ``` -------------------------------- ### Test __fastcall Free Functions with Boost Bind Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_test.cpp This C++ code demonstrates testing Boost.Bind with __fastcall free functions. It includes definitions for functions f_0 through f_9 and a test function `function_test` that uses `BOOST_TEST` to verify the results of binding these functions with various arguments and placeholders. ```C++ #include #ifndef _MSC_VER int main() { } #else #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_fastcall_test.cpp - test for bind.hpp + __fastcall (free functions) // // Copyright (c) 2002 Peter Dimov and Multi Media Ltd. // // 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) // #define BOOST_BIND_ENABLE_FASTCALL #include #include using namespace boost::placeholders; // long __fastcall f_0() { return 17041L; } long __fastcall f_1(long a) { return a; } long __fastcall f_2(long a, long b) { return a + 10 * b; } long __fastcall f_3(long a, long b, long c) { return a + 10 * b + 100 * c; } long __fastcall f_4(long a, long b, long c, long d) { return a + 10 * b + 100 * c + 1000 * d; } long __fastcall f_5(long a, long b, long c, long d, long e) { return a + 10 * b + 100 * c + 1000 * d + 10000 * e; } long __fastcall f_6(long a, long b, long c, long d, long e, long f) { return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f; } long __fastcall f_7(long a, long b, long c, long d, long e, long f, long g) { return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g; } long __fastcall f_8(long a, long b, long c, long d, long e, long f, long g, long h) { return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h; } long __fastcall f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i) { return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i; } void function_test() { using namespace boost; int const i = 1; BOOST_TEST( bind(f_0)(i) == 17041L ); BOOST_TEST( bind(f_1, _1)(i) == 1L ); BOOST_TEST( bind(f_2, _1, 2)(i) == 21L ); BOOST_TEST( bind(f_3, _1, 2, 3)(i) == 321L ); BOOST_TEST( bind(f_4, _1, 2, 3, 4)(i) == 4321L ); BOOST_TEST( bind(f_5, _1, 2, 3, 4, 5)(i) == 54321L ); BOOST_TEST( bind(f_6, _1, 2, 3, 4, 5, 6)(i) == 654321L ); BOOST_TEST( bind(f_7, _1, 2, 3, 4, 5, 6, 7)(i) == 7654321L ); BOOST_TEST( bind(f_8, _1, 2, 3, 4, 5, 6, 7, 8)(i) == 87654321L ); BOOST_TEST( bind(f_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i) == 987654321L ); } int main() { function_test(); return boost::report_errors(); } #endif ``` -------------------------------- ### Bind Member Function g5 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a member function 'g5' with different argument passing methods (pointer, value, ref) and 5 arguments. ```C++ bind(&X::g5, &x, 1, 2, 3, 4, 5)(); bind(&X::g5, x, 1, 2, 3, 4, 5)(); bind(&X::g5, ref(x), 1, 2, 3, 4, 5)(); ``` -------------------------------- ### Visitor Pattern with Boost Bind Source: https://www.boost.org/doc/libs/latest/libs/bind/bind_visitor.cpp This C++ code defines a visitor struct and uses boost::bind to apply it to a function, demonstrating a common pattern for processing data structures or function arguments. ```C++ #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_visitor.cpp - tests bind.hpp with a visitor // // Copyright (c) 2001 Peter Dimov and Multi Media Ltd. // // 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 #include #include using namespace boost::placeholders; // struct visitor { template void operator()( boost::reference_wrapper const & r ) const { std::cout << "Reference to " << typeid(T).name() << " @ " << &r.get() << " (with value " << r.get() << ")\n"; } template void operator()( T const & t ) const { std::cout << "Value of type " << typeid(T).name() << " (with value " << t << ")\n"; } }; int f(int & i, int & j, int) { ++i; --j; return i + j; } int x = 2; int y = 7; int main() { using namespace boost; visitor v; visit_each(v, bind(bind(f, ref(x), _1, 42), ref(y)), 0); } ``` -------------------------------- ### Binding Member Functions with 5 Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_mf_test.cpp Shows binding member functions 'f5' and 'g5' of class 'X' with five arguments. Covers binding using a pointer to member and `boost::ref`, passing five integer arguments. ```cpp // 5 bind(&X::f5, &x, 1, 2, 3, 4, 5)(); bind(&X::f5, ref(x), 1, 2, 3, 4, 5)(); bind(&X::g5, &x, 1, 2, 3, 4, 5)(); bind(&X::g5, x, 1, 2, 3, 4, 5)(); bind(&X::g5, ref(x), 1, 2, 3, 4, 5)(); ``` -------------------------------- ### Bind Void Member Function f2 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a void member function 'f2' with different argument passing methods (pointer, ref) and 2 arguments. ```C++ bind(&V::f2, &v, 1, 2)(); bind(&V::f2, ref(v), 1, 2)(); ``` -------------------------------- ### Bind Member Function g7 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a member function 'g7' with different argument passing methods (pointer, value, ref) and 7 arguments. ```C++ bind(&X::g7, &x, 1, 2, 3, 4, 5, 6, 7)(); bind(&X::g7, x, 1, 2, 3, 4, 5, 6, 7)(); bind(&X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); ``` -------------------------------- ### Bind with explicit return type and arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a function object with a specified return type and arguments, including placeholders. ```C++ BOOST_TEST( bind(Y(), i, _1)(k) == 38 ); BOOST_TEST( bind(Y(), i, _1, 9)(k) == 938 ); ``` -------------------------------- ### Bind Void Member Function f1 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a void member function 'f1' with different argument passing methods (pointer, ref) and 1 argument. ```C++ bind(&V::f1, &v, 1)(); bind(&V::f1, ref(v), 1)(); ``` -------------------------------- ### Binding Member Functions with 2 Arguments Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_fastcall_mf_test.cpp Shows binding member functions 'f2' and 'g2' of class 'X' with two arguments. Demonstrates binding using a pointer to member and `boost::ref`, passing two integer arguments. ```cpp // 2 bind(&X::f2, &x, 1, 2)(); bind(&X::f2, ref(x), 1, 2)(); bind(&X::g2, &x, 1, 2)(); bind(&X::g2, x, 1, 2)(); bind(&X::g2, ref(x), 1, 2)(); ``` -------------------------------- ### Bind Void Member Function f3 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a void member function 'f3' with different argument passing methods (pointer, ref) and 3 arguments. ```C++ bind(&V::f3, &v, 1, 2, 3)(); bind(&V::f3, ref(v), 1, 2, 3)(); ``` -------------------------------- ### Bind Void Member Function f0 Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Demonstrates binding a void member function 'f0' with different argument passing methods (pointer, ref). ```C++ bind(&V::f0, &v)(); bind(&V::f0, ref(v))(); ``` -------------------------------- ### Bind member function f0 (no arguments) Source: https://www.boost.org/doc/libs/latest/libs/bind/test/bind_test.cpp Illustrates binding the `f0` member function of class `X` using different ways to pass the object instance. ```C++ bind(&X::f0, &x)(); bind(&X::f0, ref(x))(); ```