2 Description of BIND usage

Source: Internet
Author: User

This article talk about bind, say bind-function technology has a period of time very tide ah, threatened to eliminate the object-oriented programming in minutes, and let gang of four design mode no longer useful, actually heard the news I am very depressed, because the design mode I have not how to learn, I see to be eliminated AH. So let's take a look at the usage of bind-function, I'm using C++11, STD's bind instead of boost bind.
One, callable object
In C + + there is a concept called callable object, so that the concept of a very different, the addition of parentheses, filling parameters can be used uniformly, it is shocking. For example, when you add a bracket and fill in the parameters,
1, the function pointer, quite called the function;
2, the function object, which quite calls the object's. Operator (params) method;
3, the member function pointer, which is preceded by the object that called it, becomes the object that called the method.
Second, using the Std::funciton template to receive the above three items
. h
Class Binddemo
{
Public
Binddemo ();
void Usefunction ();
Static double Amemberfunc (double param);
int operator () (int param);
};
Inline double afreefunc (double param);
. cpp
#include "binddemo.h"
#include <functional>
#include <iostream>
Binddemo::binddemo ()
{
Usefunction ();
}

void Binddemo::usefunction ()
{
1
Std::function<double (double) > FR1 = Afreefunc;
STD::COUT<<FR1 (5) <<std::endl;

2
Std::function<double (double) > FR2 = Binddemo::amemberfunc;
STD::COUT<<FR2 (6) <<std::endl;

3
Std::function<double (double) > FR3 = *this;
STD::COUT<<FR3 (7) <<std::endl;
}

Double Binddemo::amemberfunc (double param)
{
return param * 3;
}

int binddemo::operator () (int param)
{
return param;
}


Double Afreefunc (double param)
{
return param * 2;
}
Three function objects are defined in the Usefunction functions, which are managed by free function, class static function, function object, respectively. Unfortunately, you cannot host a class member function.

Iv. creating closures with the Std::bind function
The concept of a closure, I think in the popular sense, is that the closure of all the required content is put in, or only to set aside the individual needs of the caller to fill in the content, when the call can be unified call. It's not a popular thing to say.
. h
void Usebind ();
. cpp
void Binddemo::usebind ()
{
1
Using namespace std::p laceholders;
Std::cout<<std::bind (afreefunc,4) () <<std::endl;
Std::cout<<std::bind (Afreefunc,_1) (4) <<std::endl;

2
Std::cout<<std::bind (&binddemo::amemberfunc2,*this,4,5,6) () <<std::endl;
Std::cout<<std::bind (&binddemo::amemberfunc2,this,4,5,6) () <<std::endl;

3
Std::cout<<std::bind (&binddemo::amemberfunc,6) () <<std::endl;
Std::cout<<std::bind (&binddemo::amemberfunc,this,6) () <<std::endl;

4
Std::cout<<std::bind (*this,6) () <<std::endl;
}
It is also easy to read (if you read the first blog), the first parameter is the function pointer, notice the FREE function pointer and the member function pointer difference is OK. which
1 is a binding free function, not using placeholders or using placeholders.
2 is a binding member function, an incoming instance object can be a pointer to an instance, or it can be an instance. In particular, it is important to note that if an instance is passed in, it is re-constructing an instance object before it is worn, if it is necessary to wear a reference:
Std::cout<<std::bind (&binddemo::amemberfunc2,std::ref (*this), 4,5,6) () <<std::endl;
That is, the use of std::ref () packaging, the incoming reference in. (In fact, I wrote here to understand what is the use of std::ref, before still feel really useless, this just know that the type of the parameter is defined by the function itself, and if the function itself is a value type, then the user to wear the reference to do? Good, you can use STD::REF () packaging.
3 is bound static function, comment partial error
4 is a bound function object.
Also note the notation of the placeholder.

Five, combination of bind and function
. h
void Usebindwithfunction ();
. cpp
void Binddemo::usebindwithfunction ()
{
1
Using namespace std::p laceholders;
Std::function<double () > FC1 = Std::bind (afreefunc,4);
STD::COUT<<FC1 () <<std::endl;
Std::function<double (double) > FC2 = Std::bind (afreefunc,_1);
STD::COUT<<FC2 (2.0) <<std::endl;

2
Std::function<double () > FC3 = Std::bind (&binddemo::amemberfunc2,*this,4,5,6);
STD::COUT<<FC3 () <<std::endl;
Std::function<double (double) > FC4 = Std::bind (&binddemo::amemberfunc2,*this,_1,5,6);
STD::COUT<<FC4 (2.2) <<std::endl;
Std::function<double (double,double) > fc5 = Std::bind (&binddemo::amemberfunc2,*this,_1,_2,6);
STD::COUT<<FC5 (2.0,3.1) <<std::endl;


3
Std::function<int () > fc6 = Std::bind (*this,6);
STD::COUT<<FC6 () <<std::endl;
}
This is also written more clearly, previously said that function receives free function pointers, member variables pointers, function objects, and bind is the function of a variety of free function pointers, member variables pointers, function objects to be packaged into a style function, so that can be unified call- This is the basis of the legendary use of Function-bind to drive the design pattern out of the historical stage.
1 is a free function, 2 is a member function, and 3 is a function object.
It is important to note that the function angle brackets inside the content, and bind form the contents of the same, that is, in 1, std::function<double () > FC1 = Std::bind (afreefunc,4); Bind does not need to be passed in again, so the argument list for the type in angle brackets is void, while std::function<double (double) > FC2 = Std::bind (afreefunc,_1); Then the argument list in angle brackets is double.
It is also important to note that bind returns a pointer that can not be received with a reference when the function is received on this side.
This is a good use, given the example below.
Application of bind to variable parameters in callback function
requirements, the number of arguments to the callback function is greater than or equal to the number of function definitions.
. h
Double aMemberFunc3 (int param1,double param2);
void Callbackfunc (Std::function<double (int,double) > Func);
void Usecallbackfunc ();

Out-of-class definitions:
inline double aFreeFunc2 (int param1,double param2);

. cpp
Double binddemo::amemberfunc3 (int param1, double param2)
{
return param1 +param2;
}
void Binddemo::callbackfunc (std::function<double (int, double) > Func)
{
Std::cout<<func (5,3.2) <<std::endl;
}
void Binddemo::usecallbackfunc ()
{
Using namespace std::p laceholders;
Callbackfunc (AFREEFUNC2);
Callbackfunc (Std::bind (&binddemo::amemberfunc3,this,_1,_2));
Callbackfunc (Std::bind (&binddemo::amemberfunc2,this,2.3,_1,_2));
}
Double AFreeFunc2 (int param1, double param2)
{
return param1 + param2;
}
As you can see, the callback to the free function in Usecallbackfunc is simple, just put it in, and as long as the parameter is right, it can run. The callback of the member function is troublesome, the main is also to pass an instance object (or pointer) in, so must use bind. And the four sentence is bind I think the most important usage, the current domain of the variables passed in, will be more than the requirements (but also to meet the requirements of the type of parameters) of the content of the content can be passed in. This is actually the same as lambda usage, except that Lambda knows whether to use environment variables when writing content, and this is called to know if an environment variable is used.

2 Description of BIND usage

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.