title: constructor and destructor calls involved in object creation
body: <p>I am using the following code snippet to compare two methods for creating an object in C++.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using std::cout;
  using std::endl;
  
  class Foo {
      public:
          Foo() : x(0) { cout &lt;&lt; "In Foo constructor." &lt;&lt; endl; }
          ~Foo() { cout &lt;&lt; "In Foo destructor." &lt;&lt; endl; }
          Foo(const Foo&amp;) { cout &lt;&lt; "In Foo copy constructor." &lt;&lt; endl; }
  
          // Assignment operator.
          Foo&amp; operator=(const Foo&amp;) {
              cout &lt;&lt; "In assignment operator." &lt;&lt; endl;
              return *this;
          }
  
      private:
          int x;
  };
  
  int main() {
  
      cout &lt;&lt; "Constructing Foo 1" &lt;&lt; endl;
      Foo Foo_1;
      cout &lt;&lt; "Constructing Foo 2" &lt;&lt; endl;
      Foo Foo_2 = Foo();
  
      return 0;
  }
  </code></pre>
  
  <p>The output from this code snippet is:</p>
  
  <pre><code>  Constructing Foo 1
    In Foo constructor.
    Constructing Foo 2
    In Foo constructor.
    In Foo destructor.
    In Foo destructor.
  </code></pre>
  
  <p>I am using <strong>Visual C++ 2010</strong> (compiler version 16.x) and I am compiling the snippet using <code>cl /EHsc /W4 test.cpp</code>. In the construction of <code>Foo_2</code>, I was expecting to see an extra call to the constructor and destructor in order to create a temporary object and a call to the assignment operator in order to assign the temporary object to <code>Foo_2</code>. Can someone explain to me why this is not the case. Apologies if I am missing something very obvious here.</p>