




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年及未来5年中国高端物业管理行业市场调查研究及投资战略咨询报告
- 2025福建福州文教职业中专学校招聘1人考前自测高频考点模拟试题及答案详解(网校专用)
- 2025贵州省职工医院第十三届贵州人博会引进高层次人才和急需紧缺人才招聘13人模拟试卷附答案详解(完整版)
- 2025广东省农业科学院设施农业研究所招聘劳动合同制人员1人模拟试卷带答案详解
- 2025湖南省社会科学院(湖南省人民政府发展研究中心)招聘高层次人才14人模拟试卷及答案详解(新)
- 2025年甘肃省兰州新区石化产业投资集团有限公司急需紧缺专业技术岗位招聘14人模拟试卷附答案详解(考试直接用)
- 2025辽宁鞍山立山区教育局招聘2人模拟试卷及完整答案详解一套
- 2025广东清远市英德市招聘教师222人模拟试卷及一套答案详解
- 2025航天六院内蒙古航天红峡化工有限公司招聘30人模拟试卷及1套参考答案详解
- 2025江苏盐城工学院招聘7人模拟试卷及完整答案详解
- 新版中华民族共同体概论课件第十二讲民族危亡与中华民族意识觉醒(1840-1919)-2025年版
- 2025-2026学年人教版(2024)九年级物理全册第十四章 内能的利用(单元同步检测练习)(含答案)
- 第1课时 10的加、减法(教学设计)-2024-2025学年一年级上册数学人教版
- 2025至2030中国聚烯烃行业项目调研及市场前景预测评估报告
- 2025四川达州宣汉县国有资产管理服务中心县属国有企业招聘劳动合同职工26人笔试历年参考题库附带答案详解
- 新教科版小学1-6年级科学需做实验目录
- 传染病学课件:霍乱完整版
- 《电子商务法律法规》课程标准
- 医院关于印发《即时检验临床应用管理办法》的通知
- 三年级下册书法练习指导全册教案(湖南美术出版社)
- GB/T 17880.5-1999平头六角铆螺母
评论
0/150
提交评论