为什么这不能在CLang 7及更低版本中编译,而在CLang 8及更高版本中编译:
#include <map>
#include <string>
typedef std::map<std::string, int> TestMap;
TestMap m {
{"a", 1},
{"b", 2},
{"c", 3},
};
auto func = [](const TestMap::value_type & p) -> int { return p.second; };
auto func1 = func;
//In CLang 7 and lower copy assignment operator is not defined
func = func1;
Run Code Online (Sandbox Code Playgroud)
实际发生了什么变化?
但这可以与所有CLang版本一起编译:
auto func1 = []() { return 5;};
decltype(func1) func2 = func1;
func2 = func1;
Run Code Online (Sandbox Code Playgroud)
这里提供所有示例代码
Lambda之间有什么区别?
正如 @rafix07 在评论中提到的,你必须使用 C++20 标准进行编译。
C++20 之前的标准:
ClosureType& operator=(const ClosureType&) = delete; (until C++20)
Run Code Online (Sandbox Code Playgroud)
如果未指定捕获,则闭包类型具有默认的复制赋值运算符和默认的移动赋值运算符。否则,它有一个删除的复制赋值运算符(这包括存在捕获默认值的情况,即使它实际上没有捕获任何内容)。(自 C++20 起)
复制赋值运算符被定义为已删除(并且未声明移动赋值运算符)。闭包类型不可 CopyAssignable。(C++20 之前)ClosureType::operator=(const ClosureType&)
Run Code Online (Sandbox Code Playgroud)ClosureType& operator=(const ClosureType&) = delete; (until C++20) ClosureType& operator=(const ClosureType&) = default; (since C++20) ClosureType& operator=(ClosureType&&) = default; (only if no captures are specified) ClosureType& operator=(const ClosureType&) = delete; (since C++20) (otherwise)
https://en.cppreference.com/w/cpp/language/lambda
请参阅此处的汇编: https: //godbolt.org/z/jpCYNQ
代码示例取自评论中的@Alexey Starinsky 链接。
| 归档时间: |
|
| 查看次数: |
113 次 |
| 最近记录: |