1、Lambda的各种调用
void CTestA5::TestLambda()
{
[](){cout << "call no param lambda" << endl; }(); // Lambda调用。无参数的情况
auto testNoParam = [](){cout << "Assign and call no param lambda" << endl; }; // 赋值然后调用
testNoParam();
auto test = [](int k){ cout << "Assign and call param lambda, the param is :" << k << endl; }; // Lamada申明
test(10); // 调用
// 无参数但是有返回类型的调用
auto test2 = []()->string{string strRet = "Return Value"; cout << "Assign and call return value lambda, the return is : " << strRet << endl; return strRet; };
test2();
// 非引用和引用调用,使用局部变量
int offset = 10;
int iOther = 100;
function<int(int)> offsert_ByReference = [&](int j){return offset + j; };// 使用引用捕获变量
function<int(int)> offsert_ByValue = [=](int j){return offset + j; }; // 使用value捕获变量
offset = 20;
function<int(int)> offsert_ByValueAndRef = [=, &iOther](int j){return offset + iOther + j + local_; }; // 部分复制,部分引用
// 部分复制,部分引用,如果全部显示指定,访问this成员变量或则函数,需要增加this
function<int(int)> offsert_ByValueAndRe2 = [this, offset, &iOther](int j){return offset + iOther + j + local_; };
offset = 30;
iOther = 1000;
cout << "call lambda, use local variable, by value the value is " << offsert_ByValue(10) << endl;
cout << "call lambda, use local variable, by reference the value is " << offsert_ByReference(10) << endl;
cout << "call lambda, use local variable, by value and reference is " << offsert_ByValueAndRef(10) << endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////
2、不限制个数的模板参数
template<typename T, typename ...Arg>
class CTmpParam
{
public:
int GetSizeof()
{
return sizeof...(Arg);
}
CTmpParam() = default;
T t_;
tuple<Arg...> tuple_;
};
void CTestA5::TestTmpParam()
{
CTmpParam<int, float, double, string> CTst;
int a = CTst.t_;
float f1 = get<0>(CTst.tuple_);
string str1 = get<2>(CTst.tuple_);
int iSizeof = CTst.GetSizeof();
}