#include <iostream>
#include <typeinfo>
#include <type_traits>
#include <memory>
#include <thread>
#include <atomic>
using namespace std;
// lambda函數的語法定義:(采用了追蹤返回類型的方式聲明其返回值)
// [capture](parameters) mutable -> return-type{statement;}
// [],捕捉列表,捕捉上下文中的變量以供lambda函數使用
/*[] 表示 空捕捉
*[=] 表示值 傳遞方式捕捉所有父作用域的變量(包括this)
*[&] 表示引用傳遞方式捕捉所有父作用域的變量(包括this)
*[var] 表示值 傳遞方式捕捉變量var
*[&var]表示引用傳遞方式捕捉變量var
*[this]表示值 傳遞方式捕捉當前this指針 (this。函數體內可以使用Lambda所在類中的成員變量。)
*其它組合形式:
* */
class Test{
public:
Test(int a, int b): m_a(a),m_b(b){}
void f1(int t){
auto f = [this] {return m_a + m_b;}; // 省略參數列表,返回值由編譯器推斷為int
cout <<__FILE__<< __LINE__ << "\t" << f() + t <<endl;
}
int m_a;
int m_b;
};
int main()
{
[] {}; // 最簡lambda函數
int a = 3;
int b = 4;
// [=]
auto c = [=] {return a + b;}; // 省略參數列表,返回值由編譯器推斷為int
cout <<__FILE__<< __LINE__ << "\t" << c() << endl;
b ++; // 值捕捉注意: 它只捕捉一次。如需關聯變量,用引用
cout <<__FILE__<< __LINE__ << "\t" << c() << endl;
// [&]
auto d = [&] {return a + b;}; // 省略參數列表,返回值由編譯器推斷為int
cout <<__FILE__<< __LINE__ << "\t" << d() << endl;
b ++; // 值捕捉注意: 它只捕捉一次。如需關聯變量,用引用
cout <<__FILE__<< __LINE__ << "\t" << d() << endl;
// [this]
Test t(1,2);
t.f1(9);
return 0;
}

