---
title: "C++闭包： Lambda Functions in C++11"
author: "anzhsoft"
date: "2013-12-20T17:29:00Z"
tags: []
slug: "c-lambda-functions-in-c-11"
draft: false
lastmod: "2014-03-14T20:25:51Z"
id: 2031376
url: "https://anzhsoft.iteye.com/blog/2031376"
---

![a lambda](http://www.cprogramming.com/c++11/lambda.png)表达式无疑是C++11最激动人心的特性之一！它会使你编写的代码变得更优雅、更快速! 它实现了C++11对于支持闭包的支持。首先我们先看一下什么叫做闭包

维基百科上，对于[闭包](http://en.wikipedia.org/wiki/Closure_%28computer_science%29)的解释是：

In [programming languages](http://en.wikipedia.org/wiki/Programming_language "Programming language"), a **closure** (also **lexical closure** or**function closure**) is a[function](http://en.wikipedia.org/wiki/Function_%28computer_science%29 "Function (computer science)") or reference to a function together with a *referencing environment*—a table storing a[reference](http://en.wikipedia.org/wiki/Reference_%28computer_science%29 "Reference (computer science)") to each of the [non-local variables](http://en.wikipedia.org/wiki/Non-local_variable "Non-local variable") (also called [free variables](http://en.wikipedia.org/wiki/Free_variable "Free variable") or upvalues) of that function.[\[1\]](http://en.wikipedia.org/wiki/Closure_%28computer_science%29#cite_note-1) A closure—unlike a plain[function pointer](http://en.wikipedia.org/wiki/Function_pointer "Function pointer")—allows a function to access those non-local variables even when invoked outside its immediate[lexical scope](http://en.wikipedia.org/wiki/Lexical_scope "Lexical scope").

The concept of closures was developed in the 1960s and was first fully implemented in 1975\[[*citation needed*](http://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")] as a language feature in the [Scheme](http://en.wikipedia.org/wiki/Scheme_%28programming_language%29 "Scheme (programming language)") programming language to support lexically scoped [first-class functions](http://en.wikipedia.org/wiki/First-class_function "First-class function"). The use of closures is associated with [functional programming languages](http://en.wikipedia.org/wiki/Functional_programming_languages "Functional programming languages") such as [Lisp](http://en.wikipedia.org/wiki/Lisp_%28programming_language%29 "Lisp (programming language)") and [ML](http://en.wikipedia.org/wiki/ML_%28programming_language%29 "ML (programming language)"), as traditional imperative languages such as [Algol](http://en.wikipedia.org/wiki/Algol_%28programming_language%29 "Algol (programming language)"), [C](http://en.wikipedia.org/wiki/C_%28programming_language%29 "C (programming language)") and [Pascal](http://en.wikipedia.org/wiki/Pascal_%28programming_language%29 "Pascal (programming language)") do not support returning [nested functions](http://en.wikipedia.org/wiki/Nested_function "Nested function") as results from [higher-order functions](http://en.wikipedia.org/wiki/Higher-order_function "Higher-order function") and thus do not require supporting closures either. Many modern[garbage-collected](http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 "Garbage collection (computer science)") imperative languages support closures, such as [Smalltalk](http://en.wikipedia.org/wiki/Smalltalk "Smalltalk") (the first object-oriented language to do so)[\[2\]](http://en.wikipedia.org/wiki/Closure_%28computer_science%29#cite_note-2),[JavaScript](http://en.wikipedia.org/wiki/JavaScript "JavaScript") and [C#](http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 "C Sharp (programming language)").

简单来说，**闭包**（*Closure*）是**词法闭包**（*Lexical Closure*）或者**函数闭包**的简称，是引用了自由变量的函数。这个被引用的自由变量将和这个函数一同存在，即使已经离开了创造它的环境也不例外。所以，有另一种说法认为闭包是由函数和与其相关的引用环境组合而成的实体。

在程序设计语言中，变量可以分为**自由变量**（free variable）与**约束变量**（bound variable）两种。简单来说，一个函数里局部变量和参数都被认为是约束变量；而不是约束变量的则是自由变量。

[百度百科](http://baike.baidu.com/link?url=ioISEQIfqQWe1COsf239QToOyO5ty-TD0phwR5kemkOjzPDvJ0CV4HnRrdAUqKCz)的解释可能更加通俗易懂：

闭包是可以包含自由（未绑定到特定对象）变量的代码块；这些变量不是在这个代码块内或者任何全局上下文中定义的，而是在定义代码块的环境中定义（局部变量）。“闭包” 一词来源于以下两者的结合：要执行的代码块（由于自由变量被包含在代码块中，这些自由变量以及它们引用的对象没有被释放）和为自由变量提供绑定的计算环境（作用域）。在 Scala、Scheme、Common Lisp、Smalltalk、Groovy、JavaScript、Ruby、 Python和Lua，objective c 等语言中都能找到对闭包不同程度的支持。

在编程领域我们可以通俗的说：子函数可以使用父函数中的局部变量，这种行为就叫做闭包！

还是上code吧：还是从Hello, World!开始

```
auto func = [] { cout << "Hello, World!" << endl; } ();

func();// 调用函数输出Hello, World!
```

我们可以像创建变量一样方便的创建函数！使用STL我们会为了某个很简单的实现不得不实现一个一个的小函数，![a lambda](http://www.cprogramming.com/c++11/lambda.png)的引入可以让我们写出更紧凑的代码：

```
bool compare(int i,int j) { return (i>j); }
vector<int> v;
// using default comparison (operator <):
std::sort (v.begin(), v.end());  // using function as comp
std::sort (v.begin(), v.end(), compare); 

// using lambda
std::sort (v.begin(), v.end(), [](int i, int j){ return i > j; });

// Print the contents of the vector.
std::for_each(v.begin(), v.end(), [](int i) { cout << i << " "; });

// C++11 style: using begin(v) instead of v.begin()

std::for_each(begin(v), end(v), [](int i) { cout << i << " "; });
```

仅仅通过sort和对于vector的遍历，我们就可以看到a lambda的巨大威力。当然了，对于C++11，我们可以使用“现代风格”进行数组的遍历：

```
for( auto d : myvector ) {
  cout<< d <<" ";
}
```

另外一个快速示例：找到v里面大于x并且小于y的第一个元素。在C++11中，最简单和干净的代码就是调用一个标准函数。

```
// C++98/03: 直接编写一个循环 （使用std::find_if会非常困难）
vector<int>::iterator i = v.begin();
for( ; i != v.end(); ++i ) {
if( *i > x && *i < y ) break;
}
 
// C++11: use std::find_if
auto i = find_if( begin(v), end(v), [=](int i) { return i > x && i < y; } );
```

数组求和：

```
for_each(begin(v), end(v), [&](int n){ sum += n;)}
```

最后一个使得代码高效优雅的例子：

```
// The number of elements in the vector.
const int elementCount = 9;
// Create a vector object with each element set to 1.
vector<int> 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(begin(v) + 2, elementCount - 2, [=]() mutable throw() -> int {      
      // Generate current value.
      int n = x + y;
      // Update previous two values.
      x = y;
      y = n;
      return n;
});
```

下面对![a lambda](http://www.cprogramming.com/c++11/lambda.png)表达式语法进行详细的阐述：

![Structural elements of a lambda expression](http://i.msdn.microsoft.com/dynimg/IC251606.png "Structural elements of a lambda expression")

1\. lambda-introducer： 定义引用自由变量的方式。

\[] // 没有定义任何变量。使用未定义变量会导致错误。

\[x, &y] // x 以传值方式传入(默认)，y 以引用方式传入。

\[&] // 任何被使用到的外部变量皆隐式地以引用方式加以使用。

\[=] // 任何被使用到的外部变量皆隐式地以传值方式加以使用。

\[&, x] // x 显示地以传值方式加以使用。其余变量以引用方式加以使用。

\[=, &z] // z 显示地以引用方式加以使用。其余变量以传值方式加以使用。

2\. lambda-parameter-declaration-list： 参数列表。但是参数不可以有默认值，不可以使用变长参数，不可以有unamed arguments

3\. mutable-specification ：使得传值引入的变量可以修改。这个修改因为是修改的外部变量的拷贝，因此并不会影响它本来的值

4\. exception-specification：throw()该函数不能抛出异常。如果抛出异常，编译器将报warning C4297。 throw(...) 可以抛出异常。throw(type)可以抛出type的异常

5\. lambda-return-type-clause：如果仅有0/1个return的话可以省略。返回值可以是lambda表达式。

```
// compile with: /EHsc
#include <functional>
// The following code declares a lambda expression that returns 
// another lambda expression that adds two numbers. 
// The returned lambda expression captures parameter x by value.
auto g = [](int x) -> function<int (int)> { return [=](int y) { return x + y; }; };

// The following code declares a lambda expression that takes another
// lambda expression as its argument.
// The lambda expression applies the argument z to the function f and adds 1.
auto h = [](const function<int (int)>& f, int z) { return f(z) + 1; };

// Call the lambda expression that is bound to h. 
auto a = h(g(7), 8);
```

最后一个问题， what's the type of a lambda expression? 使用C++11增强的各类function的wrapper std::function, 下面是使用![a lambda](http://www.cprogramming.com/c++11/lambda.png)作为代理(Delegate)的例子，仅仅是例子，不要问为什么要这样用:)

```
#include <functional>

class EmailProcessor
{
public:
    void receiveMessage (const std::string& message)
    {
        if ( _handler_func ) 
        {
            _handler_func( message );
        }
        // other processing
    }
    void setHandlerFunc (std::function<void (const std::string&)> handler_func)
    {
        _handler_func = handler_func;
    }

private:
        std::function<void (const std::string&)> _handler_func;
};

//当收到Message时，我们希望有个回调函数能够处理

class MessageSizeStore
{
    MessageSizeStore () : _max_size( 0 ) {}
    void checkMessage (const std::string& message ) 
    {
        const int size = message.length();
        if ( size > _max_size )
        {
            _max_size = size;
        }
    }
    int getSize ()
    {
        return _max_size;
    }

private:
    int _max_size;
};

//我们希望每次Message到来时，统计出历史的最大长度（好无聊啊。。。）

EmailProcessor processor;
MessageSizeStore size_store;
processor.setHandlerFunc( 
        [&] (const std::string& message) { size_store.checkMessage( message ); } 
);
```

引用：

[http://www.cprogramming.com/c++11/c++11-lambda-closures.html](http://www.cprogramming.com/c++11/c++11-lambda-closures.html)

[http://rednaxelafx.iteye.com/blog/184199](http://rednaxelafx.iteye.com/blog/184199)

[http://www.cplusplus.com/reference/algorithm/sort/](http://www.cplusplus.com/reference/algorithm/sort/)

[Lambda Expression Syntax](http://msdn.microsoft.com/en-us/library/dd293603.aspx)

[http://msdn.microsoft.com/en-us/library/46h7chx6.aspx](http://msdn.microsoft.com/en-us/library/46h7chx6.aspx)

[http://msdn.microsoft.com/en-us/library/dd293599.aspx#higherOrder](http://msdn.microsoft.com/en-us/library/dd293599.aspx#higherOrder)