C++11 lambda 表達式解析


C++11 新增了很多特性,lambda 表達式是其中之一,如果你想了解的 C++11 完整特性,建議去這里這里這里,還有這里看看。本文作為 5 月的最后一篇博客,將介紹 C++11 的 lambda 表達式。

很多語言都提供了 lambda 表達式,如 Python,Java 8。lambda 表達式可以方便地構造匿名函數,如果你的代碼里面存在大量的小函數,而這些函數一般只被調用一次,那么不妨將他們重構成 lambda 表達式。

C++11 的 lambda 表達式規范如下:

[ capture ] ( params ) mutable exception attribute -> 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 表達式的參數。

一個具體的 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'; 
}

(完)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM