Boost::bind Learning

Source: Internet
Author: User

    • 1 What is a function object
    • 2Boostbind
      • Binding free functions
        • Bind all Parameters
        • Do not bind all parameters
      • Functions for binding classes
      • The value of the Boostbind binding is a copy of the value passed

1. What is a function object

Before you know the function object, you should know what a function pointer is, and the function pointer is no longer described. function objects are more flexible than function pointers and can implement inline functions.

A function Object is an object that has function functions. How can I make an object function? The answer is the overloaded operator (). Join to implement two integers to add:

class Add{public:    intoperatorintint b)    {        return a+b;    }};Add add;add(1,2);

The above is the implementation of a function object, in the execution of Add (1, 2), is actually called the overloaded operator ().

You can create a function object in Boost/function.cpp. For example

#include<boost/function.hpp>boost::function<int (intint) > F;

Represents a function object that creates a function parameter of two int and returns a value of int.
If you use generics, you can implement a generic function object

class Add{template<typename T>public:    T operator()( T a, T b)    {        return a+b;    }};Add add;add(1,2);add(1.1, 2.2);
2, Boost::bind

After you know the function object, take a look at the return value of Boost::bind,bind as a function object, and then call the function object as if it were called a function.

The usage of BIND is probably divided into two kinds, one is bound free function, that is the function that does not depend on class, and the other is binding class function. Bind the free function with a method of bind (& function name, parameter 1, parameter 2 ...); When binding a class function, use the method for bind (& Class Name:: Method Name, class instance pointer, parameter 1, Parameter 2 ...). Parameters such as parameter 1, parameter 2 in the binding parameter can be replaced with placeholder _1,_2, as illustrated below.

Binding free functions

Take the addition function as an example

int Add(intint b){    return a+b;}
Bind all Parameters

Boost::bind (&add, 1, 2) () is equivalent to calling the function Add (1, 2). Here Boost::bind (&add, 1, 2) binds the function Add and specifies the parameters at bind time, returns the function object, and then adds (), which is equivalent to invoking the function object.

Do not bind all parameters

In the above binding, Boost::bind (&add, 1, 2) specifies the parameters, equivalent to A=1, b=2. The binding can also be unbound or only part of a parameter is bound. For example
Boost::bind (&add, 1, _1) (2)
A parameter that indicates that only one parameter a=1,b is specified at bind time uses the placeholder _1, which specifies the value of the placeholder when the function object calls the function.
There are other uses, with examples to illustrate
Boost::bind (&add, _1, _2) (1, 2) is equivalent to calling the ADD (1, 2)
Boost::bind (&add, _2, _1) (1, 2) is equivalent to calling the ADD (2, 1)
Boost::bind (&add, _1, _1) (2) is equivalent to calling the ADD (2, 2)
Boost::function

Functions for binding classes

For non-static functions, use BIND (& Class Name:: Method Name, class instance pointer, parameter 1, Parameter 2 ...).
For example:

class Testbind{public: int Add (int a, int b) {return a+b; }};Testbind Test;cout<<Boost:: Bind (&Testbind:: Add,Test, 1, 2) () <<Endl;cout<<Boost:: Bind (&Testbind:: Add, &Test, 1, 2) () <<Endl;cout<<Boost:: Bind (&Testbind:: Add, _1,1, 2) (Test) <<Endl;

The above three calls are the equivalent of calling test. ADD (1, 2). As you can see, test can also be used as a parameter instead of a placeholder.

The value of the Boost::bind binding is a copy of the value passed

The function object returned by Boost::bind () holds the argument to bind, which is saved as a copy of the value. For example:

void Add(int& a){    ++a;}int n=1;boost::bind(&Add, n)();cout<<n<<endl;

After running, n is still 1. To think of N as 2, the value of the function object that needs to be returned is either a reference or a pointer pass. Use reference values to pass, plus boost::ref.

boost::bind(&Addboost::cref(n))();

When binding to a member function on an object, take the example above:

cout<<boost::bind(&testBind::Add, test,12)()<<endl;

The test object is copied here.

cout<<boost::bind(&testBind::Add, &test,12)()<<endl;

There are no copies here, there is no problem with copying objects when using references and pointers.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Boost::bind Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.