title: Assignment vs constructor in C++
body: <p>Can someone please explain why the following code prints 0 for f2? It seems like = {} somehow resolves to the int assignment operator.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  
  struct Foo
  {
      Foo() : x {-1} {}
      Foo(int x) : x{x} {}
      Foo&amp; operator=(int y) { x = y; return *this; }
      Foo&amp; operator=(const Foo&amp; f) { x = f.x; return *this; }
      int x;
  };
  
  int main()
  {
      Foo f1 = {};
  
      Foo f2;
      f2 = {};
  
      std::cout &lt;&lt; f1.x &lt;&lt; '\n';  // this prints -1
      std::cout &lt;&lt; f2.x &lt;&lt; '\n';  // this prints 0
  }
  </code></pre>