未定义 Lambda 复制分配运算符
Lambda copy assignment operator is not defined
为什么这不能用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;
到底发生了什么变化?
但这适用于所有 CLang 版本:
auto func1 = []() { return 5;};
decltype(func1) func2 = func1;
func2 = func1;
此处提供的所有示例代码
λ之间有什么区别?
正如@rafix07评论中提到的,您必须使用 C++20 标准进行编译。
C++20 之前的标准:
ClosureType& operator=(const ClosureType&) = delete; (until C++20)
如果未指定捕获,则闭包类型具有默认副本 赋值运算符和默认移动赋值 算子。否则,它有一个已删除的复制赋值运算符(此 包括存在捕获默认值的情况,即使它没有 实际上捕获了任何东西(。(自C++20起(
复制赋值运算符定义为已删除(和移动 未声明赋值运算符(。闭合类型不是 可复制可分配。(至C++20( ClosureType::operator=(const ClosureType&(
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 链接。
相关文章:
- 正在尝试重载二进制搜索树分配运算符
- 自定义先决条件对移动分配运算符有效吗
- 分配给转换运算符失败-C++
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- C++ - 没有自定义交换功能的移动分配运算符?
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 为什么在通过引用返回运算符分配时取消引用'this'指针?
- 使用 'new[]' 运算符分配内存
- 操作后通过运算符分配对象
- 新运算符分配的大小大于声明的大小.为什么
- 如何控制运算符 [] 分配的值
- 字符** 使用 new 运算符分配内存
- 新的运算符分配函数顺序连续性和初始值
- 如何释放使用 C++ "new" 运算符分配的 C 中的内存
- 只能使用CUDA中的新运算符分配有限的内存
- 使用友元运算符分配和私有变量是不可访问的
- 释放由赋值运算符C++分配的内存
- 确定C++中新运算符分配的内存大小
- 用于新运算符分配的公共内存
- 如何在C++中检索由新运算符分配的对象的地址