C++中的赋值与构造函数

use*_*667 11 c++

有人可以解释为什么以下代码为f2打印0?似乎= {}以某种方式解析为int赋值运算符.

#include <iostream>


struct Foo
{
    Foo() : x {-1} {}
    Foo(int x) : x{x} {}
    Foo& operator=(int y) { x = y; return *this; }
    Foo& operator=(const Foo& f) { x = f.x; return *this; }
    int x;
};

int main()
{
    Foo f1 = {};

    Foo f2;
    f2 = {};

    std::cout << f1.x << '\n';  // this prints -1
    std::cout << f2.x << '\n';  // this prints 0
}
Run Code Online (Sandbox Code Playgroud)