C + + supplements--LAMBDA expression principle

Source: Internet
Author: User

C + + supplements--LAMBDA expression principle

Preface

Lambda expressions are presented in the new C++11 standard. In lambda expressions, we focus on the use of it. Now let's talk about how it's implemented.

Body1. Function Objects

The object of the class is combined with parentheses to show the general behavior of the function, which can be called a function object.

#include <iostream>using namespace Std;class myclass{public://overloaded function call operator () int operator () (int i) {return i;}}; The invocation of int main () {MyClass my;//my () behaves like the same function int i = my (1);     The essence is to call My.operator () (1) cout << "i =" << i << endl;cin.get (); return 0;}
Run



This example shows that the essence of a function object is to overload the function call operator. When a class overloads the function call operator (), its object becomes a function object. This is the basis for understanding the internal implementation of a lambda expression.


2.lambda Expression Principle

Principle: The compiler generates an anonymous object for an anonymous class with a lambda expression and overloads the function call operator in the class.

We start with the simplest lambda expression, from easy to difficult

2.1 No capture list and parameter list

Auto print = []{cout << "Zhangxiang" << Endl;};
The compiler will translate this sentence into the following scenario:

Generates the corresponding class with the given lambda expression print_class{public:void operator () (void) const{cout << "Zhangxiang" << endl;}};/ /Create an object with a constructed class, and print is now a function object auto print = Print_class ();
the class name naming rules for a generated class can be variable, not necessarily.


2.2 No capture list but with parameter list

Auto add = [] (int a, int b) {return a + B;};
The compiler will translate this sentence into the following scenario:

Class Add_class{public:auto operator () (int a, int b) Const{return A + b;}}; Auto add = Add_class ();


2.3 have capture list, parameter list optional

There are two kinds of capture methods: Reference capture, value capture, and so on, it can be subdivided in different cases.

2.3.1 Value Capture

int year = 19900212;char *name = "Zhangxiang";//Use value capture to capture all defined local variables, such as year,nameauto print = [=] () {cout << year <&L T Ends << name << Endl;};
translation

int year = 19900212;char *name = "Zhangxiang"; class print_class{public://determines the argument list form of the constructor based on the capture list print_class (int year, Char *name): Year, name (name) {}void operator () (void) Const{cout << year << ends << name << Endl;} Private:int Year;char *name;}; Auto print = Print_class (A, str);
the effect is the same, it is not demonstrated.


2.3.2 Reference Capture

int year = 19900212;char *name = "Zhangxiang", auto print = [&] () {year++;cout << year << ends << name << Endl;};
translation

int year = 19900212;char *name = "Zhangxiang"; class print_class{public://because it is a reference capture, the argument list takes the form of a reference print_class (int &year, Char *&name): Year (year), name (name) {}void operator () (void) const{year++;   Compiler passed, const is invalid for reference type cout << year << ends << name << Endl;} Private:int &year;char *&name;};
after testing, the effect is the same.

2.3.3 Hybrid Capture

int year = 19900212;int shoes = 42;char *name = "Zhangxiang"; auto show = [&, shoes] () mutable{shoes++;year++;cout <& Lt Year << ends << shoes << ends << name << Endl;};
translation

int year = 19900212;int shoes = 42;char *name = "Zhangxiang"; class show_class{private:int &year;mutable int Shoes;char *&name;public:show_class (int &year, int shoes, char *&name): Year, Shoes (shoes), name (name) {}void  Operator () (void) Const{shoes++;year++;cout << year << ends << shoes << ends << name << Endl;}}; Auto Show = Show_class (year, shoes, name); show ();


By default, variables that are captured by value cannot be modified unless you add the keyword mutable after the argument list. The above code shows how mutable in the corresponding class is added.


Summary

These sample code basically shows the implementation principles of lambda expressions in various situations, and it's easy to think it over.



Directory of this column

    • C + + Supplements directory
Directory of all content
    • CCPP Blog Directory

C + + supplements--LAMBDA expression principle

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.