C++11 Lambda表达式简单解析
2014-10-27 22:20
316 查看
C++11 新增了很多特性,lambda 表达式是其中之一,如果你想了解的 C++11 完整特性,
建议去http://www.open-std.org/看看新标准!
很多语言都提供了 lambda 表达式,如 Python,Java 8
lambda 表达式可以方便地构造匿名函数,如果你的代码里面存在大量的小函数,而这些函数一般只被调用一次,那么不妨将他们重构成 lambda 表达式.
C++11 的 lambda 表达式规范如下:
其中
(1) 是完整的 lambda 表达式形式,
(2) const 类型的 lambda 表达式,该类型的表达式不能改捕获("capture")列表中的值。
(3)省略了返回值类型的 lambda 表达式,但是该 lambda 表达式的返回类型可以按照下列规则推演出来:
如果 lambda 代码块中包含了 return 语句,则该 lambda 表达式的返回类型由 return 语句的返回类型确定。
如果没有 return 语句,则类似 void f(...) 函数。
省略了参数列表,类似于无参函数 f()。
mutable 修饰符说明 lambda 表达式体内的代码可以修改被捕获的变量,并且可以访问被捕获对象的 non-const 方法。
exception 说明 lambda 表达式是否抛出异常(
Y)。
attribute 用来声明属性。
另外,capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表,具体解释如下:
此外,params 指定 lambda 表达式的参数。
2个具体的 C++11 lambda 表达式例子:
建议去http://www.open-std.org/看看新标准!
很多语言都提供了 lambda 表达式,如 Python,Java 8
lambda 表达式可以方便地构造匿名函数,如果你的代码里面存在大量的小函数,而这些函数一般只被调用一次,那么不妨将他们重构成 lambda 表达式.
C++11 的 lambda 表达式规范如下:
[ capture ] ( params )mutableexceptionattribute ->ret {body } | (1) | |
[ capture ] ( params ) -> ret {body } | (2) | |
[ capture ] ( params ) { body } | (3) | |
[ capture ] { body } | (4) |
(1) 是完整的 lambda 表达式形式,
(2) const 类型的 lambda 表达式,该类型的表达式不能改捕获("capture")列表中的值。
(3)省略了返回值类型的 lambda 表达式,但是该 lambda 表达式的返回类型可以按照下列规则推演出来:
如果 lambda 代码块中包含了 return 语句,则该 lambda 表达式的返回类型由 return 语句的返回类型确定。
如果没有 return 语句,则类似 void f(...) 函数。
省略了参数列表,类似于无参函数 f()。
mutable 修饰符说明 lambda 表达式体内的代码可以修改被捕获的变量,并且可以访问被捕获对象的 non-const 方法。
exception 说明 lambda 表达式是否抛出异常(
noexcept),以及抛出何种异常,类似于void f()throw(X,
Y)。
attribute 用来声明属性。
另外,capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表,具体解释如下:
[a,&b]a变量以值的方式呗捕获,b以引用的方式被捕获。
[this]以值的方式捕获 this 指针。
[&]以引用的方式捕获所有的外部自动变量。
[=]以值的方式捕获所有的外部自动变量。
[]不捕获外部的任何变量。
此外,params 指定 lambda 表达式的参数。
2个具体的 C++11 lambda 表达式例子:
#include <vector> #include <iostream> #include <algorithm> #include <functional> int main() { std::vector<int> c { 1,2,3,4,5,6,7 }; int x = 5; c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end()); std::cout << "c: "; for (auto i: c) { std::cout << i << ' '; } std::cout << '\n'; // the type of a closure cannot be named, but can be inferred with auto auto func1 = [](int i) { return i+4; }; std::cout << "func1: " << func1(6) << '\n'; // like all callable objects, closures can be captured in std::function // (this may incur unnecessary overhead) std::function<int(int)> func2 = [](int i) { return i+4; }; std::cout << "func2: " << func2(6) << '\n'; } //配合boost库 #include <boost/function.hpp> typedef boost::function<void(int)> fobject_t; // Now this function may accept functional objects void process_integers(const fobject_t& f); #include <assert.h> #include <deque> int main() { // lambda function with no parameters that does nothing process_integers([](int /*i*/){}); // lambda function that stores a reference std::deque<int> ints; process_integers([&ints](int i){ ints.push_back(i); }); // lambda function that modifies its content std::size_t match_count = 0; process_integers([ints, &match_count](int i) mutable { if (ints.front() == i) { ++ match_count; } ints.pop_front(); }); assert(match_count == 6); } void process_integers(const fobject_t& f) { static const int data[] = {1, 2, 3, 4, 5, 200, 0}; std::for_each(data, data + 6, f); }
相关文章推荐
- C++11 Lambda表达式简单解析
- 每天进步一点点——C++11中使用lambda表达式实现一个简单的ScopeGuard
- 稀疏矩阵线性解析库SPOOLES的简单应用
- 一个SAX解析xml的简单程序。
- 使用Digester解析XML文件简单范例
- XML文件的简单初步解析
- 解析Ajax响应结果的两种简单方法.
- javascript异步调用XML进行解析的简单实现
- C++反汇编揭秘1 一个简单的C++程序反汇编解析
- xerces-c解析中文的简单方法
- 简单的解析文件,取URL地址,并根据地址抓下页面
- DNN调度管理解析(一)-----简单介绍及其API
- 手动建立makefile简单实例解析
- 用ajax解析rss简单例子
- 一个简单CPU VHDL代码解析
- C++反汇编揭秘1 – 一个简单C++程序反汇编解析 (Rev. 3)
- 解析Petshop 3.0 简单工厂(Simple Factory)模式
- Google Maps API 2.0解析(17-GSmallMapControl和GSmallZoomControl两个简单的控件)
- 简单的XML解析程序
- demo : 简单的 xslt 递归解析 xml 成 tree