Example explains lambda expression __c++ in C + +

Source: Internet
Author: User
Tags modifier
Test environment Windows 7 vs2013

The lambda expression in C + + 11 is used to define and create anonymous function objects to simplify the programming effort. For example, GPU programming is commonly used.

One of the simplest Lamada expression programs

#include <functional>
#include <iostream>

using namespace std;
int main ()
{
function<void (void) > Fun = [] () {cout << "Hello lambda" <<endl;};//Here you can also use Auto    Variable
fun ();
Cin.get ();
return 0;
}

Use in STL

#include <functional>
#include <vector>
#include <algorithm>
#include <iostream >

using namespace std;
int main ()
{
	vector<int> vec_int;

	Vec_int.push_back (1);
	Vec_int.push_back (2);
	Vec_int.push_back (3);
	function<void (int) >  can also use Auto
	function<void (int) >  print_fun = [] (int item) {cout< <item<<endl; }; int  Item function parameter
	for_each (Vec_int.begin (), Vec_int.end (), print_fun);
	Cin.get ();

	return 0;
}

The syntactic form of a lambda is as follows:

[function Object Parameters] (operator overloaded function parameter) mutable or exception declaration-> return value type {function Body}

As you can see, the lambda is divided into five main parts:

A. [function object parameter] that identifies the beginning of a lambda, which must exist and cannot be omitted. A function object parameter is a constructor that is passed to a function object class that is automatically generated by the compiler.

Function object parameters can only use local variables that are visible in the scope of the lambda when the lambda is defined, including this in the class where the lambda is located. The function object argument has the following form:
1, empty. No function object parameters are used.
2, =. The body of a function can use all the visible local variables within the scope of the lambda, including this in the class where the lambda is located, and is the value-passing method (equivalent to the compiler automatically passing all local variables by value).
3, &. The body of a function can use all the visible local variables within the scope of the lambda, including this in the class where the lambda is located, and is a reference pass (equivalent to the compiler automatically passing all local variables by reference).
4, this. Function bodies can use the member variables in the class where the lambda is located.
5, A. Pass a by value. When passed by value, the function body cannot modify the copy of a passed in, because the function is const by default. To modify the copy of a that is passed in, you can add a mutable modifier.
6, &a. Pass a by reference.
7, A, &b. Pass a by value, and B is passed by reference.
8, =, &a, &b. Except for A and b passing by reference, the other parameters are passed by value.
9, and, A, B. Except for A and b passing by value, the other parameters are passed by reference.

#include <functional> #include <iostream> using namespace std;
	Class Test {public:int num; Public:test () {num = 100;
		} void Lambda_test () {int i_a = 3;
		
		int i_b = 5; Auto FUN1 = [] (int v) {cout <<v<<endl;};//does not capture any arguments, where int v corresponds to a function parameter//Auto FUN1 = [] (int v) {cout << This->num<<i_a<<endl; }; Error: The enclosing function local variable cannot be referenced in the lambda body unless it is in the Capture list fun1 (i_a),//By the function call//= the copy references this, and all local variables of the current block statement, not assignable, but can read auto fun2 = [=] (
		{cout << "num:" <<this->num << "i_a:" <<i_a<< "I_b:" <<i_b<<endl;};

		Fun2 ();
		& manipulate local variables by reference, this, you can assign values, you can read auto FUN3 = [Ampersand] () {This->num = = I_a = 20;
		Fun3 ();
		cout << "Modified num:" << this->num << "i_a:" << i_a << "i_b:" << i_b << Endl; Specifies that the parameters of the i_a can be read-writable, I_b read-only, This->num cannot access the auto Fun4 = [&i_a, I_b] () {i_a = n cout << "i_a:" << i_a & lt;< "I_b:" << i_b << Endl;
		};
		
		
	Fun4 ();
}
};
	int main () {test test;

	Test.lambda_test ();
	Cin.get ();
return 0;
 }

Second, (operator overloaded function parameters), identify overloaded () operator parameters, without parameters, this part can be omitted. Parameters can be passed by value (such as: (A, b)) and by reference (such as: (&a, &b)). Similar to the function is not an example.
Three, mutable or exception declarations, this part can be omitted. When you pass a function object parameter by value, you can modify the copy passed in by value (Note that you can modify the copy instead of the value itself) by adding the mutable modifier. The exception declaration is used to specify an exception thrown by a function, such as an exception that throws an integer type, which can be used with throw (int).

#include <functional>
#include <iostream>

using namespace std;


int main ()
{

int num = ten;
Mutable note is the ability to modify the copy instead of the value itself
auto fun = [num] () mutable throw ()-> int{num = one cout << "lambda num:" <& Lt Num << Endl;    };
Fun ();
cout << "num:" << num << Endl;
Cin.get ();
return 0;
}

-> returns a value type that identifies the type of the function return value, which can be omitted when the return value is void or where there is only a return in the body of the function (when the compiler can automatically infer the returned value type).
Five, {function Body}, to identify the implementation of the function, this part can not be omitted, but the function body can be empty.

#include <functional>
#include <iostream>

using namespace std;

int main ()
{

	//
	Auto FUN1 = [] (double A)->double{A + = 1.1;; return A;};
	COUT<<FUN1 (1.2) <<endl;
	Auto fun2 = [] (double A)->decltype (a) {a + = 1.1;; return A;};
	COUT<<FUN2 (1.2) <<endl;

	Cin.get ();
	return 0;
}



References: http://www.cnblogs.com/hujian/archive/2012/02/14/2350306.html

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.