为什么 =default on 运算符 = 在有 const 成员时编译

Why does =default on operator= compile when there is a const member?

本文关键字:成员 编译 const 运算符 default on 为什么 在有      更新时间:2023-10-16
class Foo {
public:
  Foo& operator=(const Foo&) = default;
private:
  const int i = 0;
};

为什么=default允许那里?它编译时没有错误。我认为=default应该失败,因为它不可能分配给const变量?

到底发生了什么?

当函数无法生成时(在这种情况下),= default将生成它作为 = delete d 代替。如果尝试使用该赋值运算符,编译器应生成错误。