




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Lambda Expression SyntaxVisual Studio 2010 This topic describes the syntax of lambda expressions. It provides an example that demonstrates the structural elements of a lambda expression and how these elements relate to the example.The following program uses lambda expressions with two STL algorithms: generate_n and for_each. The lambda expression that appears in the call to the generate_n function assigns an element of a vector object to the sum of the previous two elements. The lambda expression that appears in the call to the for_each function prints an element of the same vector object to the console.Copy/ lambda_structure.cpp/ compile with: /EHsc#include #include #include using namespace std;/ The number of elements in the vector.const int elementCount = 9;int main() / Create a vector object with each element set to 1. vector v(elementCount, 1); / These variables hold the previous two elements of the vector. int x = 1; int y = 1; / Assign each element in the vector to the sum of the / previous two elements. generate_n(v.begin() + 2, elementCount - 2, =() mutable throw() - int / Generate current value. int n = x + y; / Update previous two values. x = y; y = n; return n; ); / Print the contents of the vector. for_each(v.begin(), v.end(), (int n) cout n ; ); cout endl; / Print the local variables x and y. / The values of x and y hold their initial values because / they are captured by value. cout x y type-idThe following section describes how the grammar relates to the example in the introduction.Properties of Lambda Expressions The following illustration maps the grammar to the example.The callouts in the illustration are as follows:1. lambda-introducer (referred to as capture clause later in this topic)2. lambda-parameter-declaration-list (referred to as parameter list later in this topic)3. mutable-specification (referred to as mutable specification later in this topic)4. exception-specification (referred to as exception specification later in this topic)5. lambda-return-type-clause (referred to as return type later in this topic)6. compound-statement (referred to as lambda body later in this topic)The following sections describe the grammar in more detail.Capture ClauseA lambda expression can access any variable that has automatic storage duration and that can be accessed in the enclosing scope. The capture clause specifies whether the body of the lambda expression accesses variables in the enclosing scope by value or by reference: variables that have the ampersand (&) prefix are accessed by reference and variables that do not have the & prefix are accessed by value. The empty capture clause, , indicates that the body of the lambda expression accesses no variables in the enclosing scope.The default capture mode specifies whether capture variables that you do not explicitly specify are captured by value or by reference. You can specify the default capture mode (capture-default in the syntax) by specifying & or = as the first element of the capture clause. The & element specifies that the body of the lambda expression accesses all captured variables by reference unless you explicitly specify otherwise. The = element specifies that the body of the lambda expression accesses all captured variables by value unless you explicitly specify otherwise. For example, if the body of a lambda expression accesses the external variable total by reference and the external variable factor by value, then the following capture clauses are equivalent:Copy &total, factor&, factor=, &totalYou can use lambda expressions in the body of a class method. Pass the this pointer to the capture clause to provide access to the methods and data members of the enclosing class. For an example of how to use lambda expression with class methods, see Example: Using a Lambda Expression in a Method in the topic Examples of Lambda Expressions.Parameter ListThe parameter list for a lambda expression resembles the parameter list for a function, with the following exceptions: The parameter list cannot have default arguments. The parameter list cannot have a variable-length argument list. The parameter list cannot have unnamed parameters.A lambda expression can take another lambda expression as its argument. For more information, see Higher-Order Lambda Expressions in the topic Examples of Lambda Expressions.The parameter list for a lambda expression is optional. You can omit the parameter list if you do not pass arguments to the lambda expression and you do not provide the mutable-specification, exception-specification, and lambda-return-type-clause elements. The following example shows a lambda expression that omits the parameter list:Copy / lambda_parameter_list.cppint main() int x = 4; int y = 5; int z = = return x + y; ();Mutable SpecificationThe mutable specification part enables the body of a lambda expression to modify variables that are captured by value. The preceding example uses the mutable keyword so that the body of the lambda expression can modify its copies of the external variables x and y, which the lambda expression captures by value. Because the lambda expression captures the variables x and y by value, their values remain 1 after the call to generate_n.Exception SpecificationYou can use the throw() exception specification to indicate that the lambda expression does not throw any exceptions. As with regular functions, the Visual C+ compiler generates warning C4297 if a lambda expression declares the throw() exception specification and the lambda body throws an exception, as shown in the following example:Copy / throw_lambda_expression.cppint main() / C4297 expected () throw() throw 5; ();For more information about exception specifications, see Exception Specifications.Return TypeThe return type part of a lambda expression resembles the return type part of an ordinary method or function. However, the return type follows the parameter list and you must include - before the return type.You can omit the return type part of a lambda expression if the lambda body contains a single return statement or the lambda expression does not return a value. If the lambda body consists of a single return statement, the compiler deduces the return type from the type of the return expression. Otherwise the compiler deduces the return type to be void.A lambda expression can produce another lambda expression as its return value. For more information, see Higher-Order Lambda Expressions in the topic Examples of Lambda Expressions.Lambda BodyThe lambda body part of a lambda expression can contain anything that the body of an ordinary method or function can contain. The body of both an ordinary function and a lambda expression can access the following types of variables: Parameters Locally-declared variables Class data members (when declared inside a class) Any variable that has static storage duration (for example, global variables)In addition, a lambda expression can access variables that it captures from the enclosing scope. A variable is explicitly captured if it appears in the capture clause of the lambda expression. Otherwise, the variable is implicitly captured. The body of the lambda expression uses the default capture mode to access variables that are implicitly captured.The lambda_parameter_list.cpp example implicitly captures the local variables x and y by value. The following example contains a lambda expression that explicitly captures the variable n by value and implicitly captures the variable m by reference:Copy / captures_lambda_expression.cpp/ compile with: /EHsc#include using namespace std;int main() int m = 0, n = 0; &, n (int a) mutable m = +n + a; (4); cout m endl n endl;This example prints the following to the console:Copy 50Because the variable n is captured by value, its value remains 0 after the call to the lambda expression.Although a lambda expression can only capture variables that have automatic storage duration, you can use variables that have static storage duration in the body of a lambda expression. The following example uses the generate function and a lambda expression to assign a value to each element in a vector object. The lambda expression modifies the static variable to generate the value of the next element.Copy / lambda_static_variable.cpp/ compile with: /c /E
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年度浙江省二级造价工程师之建设工程造价管理基础知识全真模拟考试试卷B卷含答案
- 2024年度浙江省二级造价工程师之土建建设工程计量与计价实务模拟考试试卷B卷含答案
- 胃肠道反应护理
- 早产儿贫血的护理与治疗
- 中西餐烹饪课件
- 种植二期愈合基台护理
- 山东轻工面试题及答案
- 瑞幸招聘面试题及答案
- 魔鬼视觉测试题及答案
- 重症颅脑损伤病人的护理常规
- 2025年广东省广州市南沙区中考二模道德与法治试题
- 2025届重庆市普通高中学业水平选择性考试预测历史试题(含答案)
- 2025-2030中国眼底照相机行业市场发展趋势与前景展望战略研究报告
- 2024年深圳市大鹏新区区属公办中小学招聘教师真题
- 人教版小学语文四年级下册作文范文2
- 大学语文试题及答案琴
- 红十字会资产管理制度
- T/CSPSTC 112-2023氢气管道工程施工技术规范
- 2025届四川成都锦江区数学七下期末质量检测试题含解析
- 无人机飞行器结构与性能试题及答案
- 《蔚来汽车》课件
评论
0/150
提交评论