#include<iostream>
using namespace std;
static string str1="this is 全局静态变量";
string str2="this is 全局变量";
void funtion1();
int main(){
string str3="this is 局部变量";
static string str4="this is 静态局部变量";
auto temp=[](int a)->bool{//[]中可以有值捕获和引用捕获,顾名思义,值捕获不可以修改其值
//反之,引用捕获可以修改并且。没有参数的话默认不捕获变(但是可以
//获得全局变量)
//(template a,...) 表示调用lamdba表达式需要传入的参数
//->后面是lambda返回值的类型
//大括号中是lamdba表达式的执行语句
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str4<<endl;
return true;
};
temp(1);
}
值捕获(CLion会直接报错,如果要修改值的话会报错,不让修改)我们只看获得到当前域中的值吧
void funtion1(){
int tep;
auto temp=[=](int a)->bool {
cout<<"tep value is"<<tep<<endl;
cout<<str1<<endl;
cout<<str2<<endl;
return false;
};
temp(2);
}
引用捕获
void funtion1(){
int tep;
auto temp=[&](int a)->bool {
cout<<"tep value is"<<tep<<endl;
tep=999;
cout<<"tep new value is:"<<tep<<endl;
cout<<str1<<endl;
cout<<str2<<endl;
return false;
};
temp(2);
}