C++ Weekly - Episode 149 脱水版: C++20's Lambda Usability Changes
C++20 Lambda 实用性的改变
- C++20 Lambda 具有默认构造函数
auto l = [] { return 5; };
decltype(l) new_lambda;
该块代码在 C++17 会提示编译错误, 无匹配的构造函数:
error: no matching constructor for initialization of 'decltype(l)' (aka '(lambda at :11:13)')
- C++20 Lambda 允许赋值操作
auto l = [] { return 5; };
auto m = l;
l = m;
该块代码在 C++17 会提示编译错误, 拷贝赋值操作被标记成 delete
error: object of type '(lambda at :11:13)' cannot be assigned because its copy assignment operator is implicitly deleted
- C++20 支持未经求值的 lambda 表达式
decltype([]{}) n;
该块代码在 C++17 会提示编译错误, Lambda 表达式处于未经求的表达式
error: lambda expression in an unevaluated operand