C ++ TR1 Function Bind, tr1functionbind

Source: Internet
Author: User

C ++ TR1 Function Bind, tr1functionbind

Before the emergence of C ++ 11, C ++ events were generally implemented through callback, such as void (* func) (int, int, int ), it is actually a function pointer. When called in C, the function name is directly written in the parameter list. In C ++, most callbacks need to be defined as static. That is, static functions. By: the scope operator, called in the form.

Of course, before the emergence of C ++ TR11, earlier functions and Bind were available in the open-source library boost, C ++ 11 tr1 draws on or directly uses the template in the boost library.

Now let's talk about the Function template in C ++ tr1, such as typedef std: tr1: function <long (const char *, unsigned int)> MSGEvent; this function is actually the definition of the previously called callback function. After using the function template, you can operate like an object and provide convenience for the subsequent bind.

C ++ tr1 bind is the same as boost bind, in the form of bind (Function fn, T1 t1, T2 t2 ,..., TN); the front name is the function name, followed by the parameter. The advantage of bind is that you can join a global function, a static function, or a member function of a class. Unlike the previous callback, you can only use static functions.

A lot of examples online, you can look at http://www.cnblogs.com/satng/archive/2011/04/29/2138804.html


Use of C ++ Boost library std: tr1: bind

You use the function when declaring the statement, and how to use the bind when defining the statement, or use the function as the declaration. A function object is returned after the bind function is bound.

Javascript: For the Functionprototypebind () method

Function. prototype. bind = function (thisArg) {var fn = this, slice = Array. prototype. slice, args = slice. call (arguments, 1); // arguments1 var a1 = arguments; return function () {alert (a1 = arguments); // judge whether the return fn is the same. apply (thisArg, args. concat (slice. call (arguments); // arguments2 }}; (function (){}). bind () (2); // always alert returns false, not. The first arguments is only thisArg, and the second is the arguments of the function returned.

Function. prototype. bind = function (thisArg) {var fn = this, slice = Array. prototype. slice, args = slice. call (arguments, 1); // arguments1 alert (arguments [0]); // alert returns 1 return function () {alert (arguments [0]); // alert outputs 2 return fn. apply (thisArg, args. concat (slice. call (arguments); // arguments2 }}; (function (){}). bind (1) (2 );

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.