写在前面
1、本文内容 cpp lambda表达式笔记 2、转载请注明出处: https://blog.csdn.net/qq_41102371/article/details/124122408
void lambda_prac() {
{
auto x = [](int val)
{
return val > 3;
};
std::cout << x(5) << std::endl;
}
{
auto x = [](int val) ->float
{
if (val > 3) {
return 3.0;
}
else {
return 1.5f;
}
};
std::cout << x(5) << std::endl;
}
{
int y = 10;
auto x = [y](int val) mutable
{
++y;
return val > y;
};
std::cout << x(5) << std::endl;
std::cout << y << std::endl;
}
{
int y = 10;
auto x = [&y](int val)
{
++y;
return val > y;
};
std::cout << x(5) << std::endl;
std::cout << y << std::endl;
}
{
static int z = 10;
auto x = [](int val)
{
return val > z;
};
std::cout << x(5) << std::endl;
}
{
int y = 10;
int z = 3;
auto x = [&y,z](int val)
{
++y;
return val > z;
};
std::cout << x(5) << std::endl;
std::cout << y << std::endl;
}
{
int y = 10;
int z = 3;
auto x = [=](int val) mutable
{
++y;
return val > z;
};
std::cout << x(5) << std::endl;
std::cout << y << std::endl;
}
{
int y = 10;
int z = 3;
auto x = [&](int val)
{
++y;
return val > z;
};
std::cout << x(5) << std::endl;
std::cout << y << std::endl;
}
{
int y = 10;
int z = 3;
auto x = [&,z](int val)
{
++y;
return val > z;
};
std::cout << x(5) << std::endl;
std::cout << y << std::endl;
}
{
struct Str {
auto fun() {
int val = 3;
auto lam = [val, this]()
{
return val > x;
};
return lam();
}
int x;
};
Str s;
s.fun();
}
{
int x = 3;
auto lam = [y = x](int val) {
return val > y;
};
}
{
int x = 3;
int y = 10;
auto lam = [z=x+y](int val) {
return val > z;
};
}
{
auto lam = [](int val) constexpr
{
return val + 1;
};
constexpr int val = lam(100);
}
{
auto lam = [](int val) consteval
{
return val + 1;
};
constexpr int val = lam(100);
}
{
auto lam = []<typename T>(T val)
{
return val + 1;
};
}
}
reference
https://en.cppreference.com/w/
完
如有错漏,敬请指正 --------------------------------------------------------------------------------------------诺有缸的高飞鸟202204
|