title: return object created in function. mingw and cl difference
body: <p>I have next code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Example
  {
  public:
      Example()
      {
          std::cout
              &lt;&lt; "constructor: "
              &lt;&lt; std::hex
              &lt;&lt; this
              &lt;&lt; std::endl;
      }
  
      ~Example()
      {
          std::cout
              &lt;&lt; "destructor:  "
              &lt;&lt; std::hex
              &lt;&lt; this
              &lt;&lt; std::endl;
      }
  
      static Example foo()
      {
          Example ex;
  
          std::cout
              &lt;&lt; "foo:         "
              &lt;&lt; std::hex
              &lt;&lt; &amp;ex
              &lt;&lt; std::endl;
  
          return ex;
      }
  };
  
  
  int
  main()
  {
      Example ex = Example::foo();
  }
  </code></pre>
  
  <p>Mingw compiled program says:
  <code>
  constructor: 0x22fe4f
  foo:         0x22fe4f
  destructor:  0x22fe4f
  </code></p>
  
  <p>That is expected result for me programming only with <code>wingw</code> and <code>g++</code>
  But when I tried to use <code>Microsoft cl</code> I got this:
  <code>
  constructor: 00000000001BF750
  foo:         00000000001BF750
  destructor:  00000000001BF750
  destructor:  00000000001BF790
  </code></p>
  
  <p>Two destructor calls? Only one constructor call? Ok, <code>foo</code> create it's own object and call <code>copy</code> constructor on <code>return</code>. But why I need to copy it when compiler can put object in <code>main</code> function stack like mingw and put <code>out</code> pointer as argument to <code>foo</code> function by itself?
  Like this:</p>
  
  <pre><code>    // no return but `out` reference
      static void foo(Example &amp;ex)
      {
          std::cout
              &lt;&lt; "foo:         "
              &lt;&lt; std::hex
              &lt;&lt; &amp;ex
              &lt;&lt; std::endl;
      }
  };
  
  
  int
  main()
  {
      Example ex; // allocate memory in stack
      Example::foo(ex); // process/fill it
  }
  </code></pre>
  
  <p>What's I do wrong or is there are any way to not call <code>copy</code> constructor and not writting code with <code>out</code> references?</p>