C + + one function

Source: Internet
Author: User



function is a template for a set of functional object wrapper classes, which implements a generic callback mechanism. Functions are similar to function pointers in that they allow the user to have greater flexibility in the implementation of the target, that is, the target can be either a normal function or a member function of a function object and a class, and it can add state to the function.

When declaring a function, you need to give the type of the returned value and the type of each parameter of the wrapped functional object. For example, declaring a function that returns a bool type and accepts an int type and a float type parameter can look like this:


Function<bool (int, float) > F;
Here is a brief introduction to some of the more important interfaces of function.

function ();
The default constructor, which creates an empty function object. If an empty function is called, an exception of type Bad_function_call will be thrown.


Template <typename f> function (F g);
The generic constructor accepts a compatible function object, which is a function or function object whose return type is either the same as the return type of the constructed function, or it can be implicitly converted, and its arguments must be the same as the parameter type of the function being constructed. Or it can be implicitly converted. Note that you can also use a different function instance to construct. When this is done, and function g is empty, the constructed function is also empty. Using an empty function pointer and an empty member function pointer can also produce empty functions. If you do this, and function g is empty, the constructed function is also empty. Using an empty function pointer and an empty member function pointer can also produce empty functions.


Template <typename f> function (reference_wrapper<f> g);
This constructor is similar to the previous one, but the function object it accepts is wrapped in a reference_wrapper to avoid passing a value to produce a copy of a function or function object. This also requires that the function object be compatible with the signature of the functions.


function& operator= (const function& g);
The assignment operator holds a copy of the function or function object in G; If G is null, the assigned function will be empty.


Template<typename f> function& operator= (F g);
This generic assignment operator accepts a compatible function pointer or function object. Note that you can also assign a value with another function instance (with a different but compatible signature). This also means that if G is another function instance and is empty, then the function after the assignment is also empty. Assigning an empty function pointer or an empty member function pointer also causes the function to be empty.


BOOL empty () const;
This member function returns a Boolean value that indicates whether the function has an object that functions or functions. If there is a target function or function object that can be called, it returns false. Because a function can be tested in a Boolean context or compared with 0, this member function may be canceled in a future version of the library, and you should avoid using it.


void Clear ();
This member function clears functions, that is, it is no longer associated with a function or function object. If the function is already empty, the call has no effect. After the call, the function must be empty. The preferred method for making a function empty is to assign 0 to it, and clear may be canceled in a future version of the library.


Result_type operator () (ARG1 A1, Arg2 A2, ..., ArgN an) const;
The Invoke operator is the method that invokes the function. You cannot call an empty function, which throws an Bad_function_call exception. The execution of the calling operator invokes the function or function object in functions and returns its result.
The following is a reference code that uses function to wrap common functions, function objects, and member functions of a class.

1. Common functions



1 int Add (int x, int y)
2
3 {
4 return x+y;
3;
6 Function<int (int,int) > f = Add;
7 int z = f (2, 3);


2. Function Object



1 class Cstudent
2 {
3 Public:
4 void Operator () (string strName, int nAge)
5 {
6 cout << strName << ":" << nAge << Endl;
7}
8};
9
Ten cstudent Stu;
Function<void (string, int) > f = stu;
F ("Mike", 12);


3. member functions of a class



1 struct TADD
2 {
3 int Add (int x,int y)
4 {
5 return x+y;
6}
7};
8
9 Function<int (TADD *, int, int) > f = tadd::add;
Ten TADD TAdd;
One f (&tadd, 2, 3); If the previous template parameter is a pass-through or reference, you can directly pass in the Tadd


Let's look at the use of functions to preserve the state of the function object. Consider the following code:



1 class CAdd
2 {
3 Public:
4 CAdd (): m_nsum (0) {NULL;}
5 int operator () (int i)
6 {
7 M_nsum + = i;
8 return m_nsum;
9}
10
int Sum () const
12 {
return m_nsum;
14}
15
Private:
M_nsum int;
18};
19
int main ()
21 {
CADD add;
Function<int (int) > f1 = add;
Function<int (int) > F2 = add;
cout << F1 (Ten) << "," << F2 (+) << "," << Add. Sum () << Endl;
return 0;
27}


It may not be the same as you might think, the output of the above program is: 10,10,0. We assign the same function object to two functions, and then call the two functions separately, but the state of the m_nsum in the function object is not maintained, what is the problem? This is because the default behavior of function is to copy an object that is passed to it, so F1 and F2 are all copies of the Add object, and the value in the Add object is not modified after calling F1 and F2.
The ref and CREF functions are provided in C + + 11 to provide references to objects and frequently-referenced wrappers. In order for function to correctly preserve the state of the functions object, we can modify the code as follows:


1 CADD add;
2 function<int (int) > F1 = ref (add);
3 function<int (int) > F2 = ref (add);
In addition, when assigning a value between two functions, if the source function holds a copy of the function object, the target function holds a copy of the function object as well, and if the source function holds a reference to the function object, The target function also holds a reference to the functional object.



Reprint http://www.cnblogs.com/hujian/archive/2012/12/07/2807605.html

If you have copyright issues, please contact QQ 858668791

C + + one function

Related Article

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.