Use of the function of the boost library

Source: Internet
Author: User



The function of the boost library is a template for a set of functional object wrapper classes, which implements a generic callback mechanism. The advantage of the Boost library's function is that it allows the user to have greater flexibility in the implementation of the target, that is, the target can be either a normal function (free function) or a function object and a class member function, and it can add state to the function.
Using the function of the boost library is a good way to blend in with your existing code. In addition, function can be used in conjunction with the Boost library's bind and lambda, which greatly expands the scope of function. The function library supports a maximum of 10 functions with a header file of function.hpp or FUNCTIONX.HPP (where x is an integer between 0 and 10). If your program only uses the function of the x parameter, then you can include only the corresponding functionx.hpp, of course, you can also include only FUNCTION.HPP (it contains all the FUNCTIONX.HPP).
The specific usage of the function library is described below.
Common function (free function) usage
int Add (int x, int y)
{
return x+y;
}
Function<int (Int,int) > F; or function2<int, int, int> F;
f = Add;
Cout << "F (2, 3) =" << f (2, 3) << Endl;
where the 1th int of the template parameter is the return value type, and the latter two are the parameter types.
Function Object Usage
Class Cstudent
{
Public
void operator () (string strName, int nAge)
{
cout << strName << ":" << nAge << Endl;
}
};
Function<void (string, int) > F; or function2<void, String, int> F;
Cstudent Stu;
f = stu;
Cout << "f (\" hello\ ", +) =" << f ("Hujian") << Endl;
member function usage
struct TADD
{
int Add (int x,int y)
{
return x+y;
}
};
TADD TAdd;
Function<int (TAdd *,int,int) > F; or Function<int (TAdd &,int,int) > F;
f = & Tadd::add;
cout << F (&tadd, 2, 3); If the previous template parameter is a value or reference, you can directly pass in the Tadd
Where the template parameter's Tadd * is a pointer to an instance of the class, you can also pass in an instance of the class or its reference, except when the call requires a corresponding modification.
When using function, you can use the empty function or compare with 0 to determine whether it points to a valid function. If function does not point to a valid function, the call throws an Bad_function_call exception. The function's clear function can make it no longer associated with a function or function object, and if it itself is empty, calling the functions will not cause any problems.



Reprint http://www.cnblogs.com/hujian/archive/2009/06/04/1495813.html

If you have copyright issues, please contact qq:858668791

Use of the function of the boost library

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.