title: How to allow copy elision construction for C++ classes (not just POD C structs)
body: <p>Consider the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  
  struct A
  {
    A() {}
    A(const A&amp;) { std::cout &lt;&lt; "Copy" &lt;&lt; std::endl; }
    A(A&amp;&amp;) { std::cout &lt;&lt; "Move" &lt;&lt; std::endl; }
  };
  
  template &lt;class T&gt;
  struct B
  {
    T x;
  };
  
  #define MAKE_B(x) B&lt;decltype(x)&gt;{ x }
  
  template &lt;class T&gt;
  B&lt;T&gt; make_b(T&amp;&amp; x)
  {
    return B&lt;T&gt; { std::forward&lt;T&gt;(x) };
  }
  
  int main()
  {
    std::cout &lt;&lt; "Macro make b" &lt;&lt; std::endl;
    auto b1 = MAKE_B( A() );
    std::cout &lt;&lt; "Non-macro make b" &lt;&lt; std::endl;
    auto b2 = make_b( A() );
  }
  </code></pre>
  
  <p>This outputs the following:</p>
  
  <blockquote>
    <p>Macro make b<br>
    Non-macro make b<br>
    Move  </p>
  </blockquote>
  
  <p>Note that b1 is constructed without a move, but the construction of b2 requires a move.</p>
  
  <p>I also need to type deduction, as <code>A</code> in real life usage may be a complex type which is difficult to write explicitly. I also need to be able to nest calls (i.e. <code>make_c(make_b(A()))</code>).</p>
  
  <p>Is such a function possible?</p>
  
  <p><b>Further thoughts:</b></p>
  
  <blockquote>
    <p><i>N3290 Final C++0x draft page 284:</i></p>
    
    <p>This elision of copy/move operations,
    called copy elision, is permitted in
    the following circumstances:</p>
    
    <p>when a temporary class object <b>that has
    not been bound to a reference</b> (12.2)
    would be copied/moved to a class
    object with the same cv-unqualified
    type, the copy/move operation can be
    omitted by constructing the temporary
    object directly into the target of the
    omitted copy/move</p>
  </blockquote>
  
  <p>Unfortunately this seems that we can't elide copies (and moves) of function parameters to function results (including constructors) as those temporaries are either bound to a reference (when passed by reference) or no longer temporaries (when passed by value). It seems the only way to elide all copies when creating a composite object is to create it as an aggregate. However, aggregates have certain restrictions, such as requiring all members be public, and no user defined constructors.</p>
  
  <p>I don't think it makes sense for C++ to allow optimizations for POD C-structs aggregate construction but not allow the same optimizations for non-POD C++ class construction.</p>
  
  <p>Is there any way to allow copy/move elision for non-aggregate construction?</p>
  
  <p><b>My answer:</b></p>
  
  <p>This construct allows for copies to be elided for non-POD types. I got this idea from <a href="https://stackoverflow.com/questions/5877726/how-to-allow-copy-elision-construction-for-c-classes-not-just-pod-c-structs/6078471#6078471">David Rodríguez's answer</a> below. It requires C++11 lambdas. In this example below I've changed <code>make_b</code> to take two arguments to make things less trivial. There are no calls to any move or copy constructors.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  
  struct A
  {
    A() {}
    A(const A&amp;) { std::cout &lt;&lt; "Copy" &lt;&lt; std::endl; }
    A(A&amp;&amp;) { std::cout &lt;&lt; "Move" &lt;&lt; std::endl; }
  };
  
  template &lt;class T&gt;
  class B
  {
  public:
    template &lt;class LAMBDA1, class LAMBDA2&gt;
    B(const LAMBDA1&amp; f1, const LAMBDA2&amp; f2) : x1(f1()), x2(f2()) 
    { 
      std::cout 
      &lt;&lt; "I'm a non-trivial, therefore not a POD.\n" 
      &lt;&lt; "I also have private data members, so definitely not a POD!\n";
    }
  private:
    T x1;
    T x2;
  };
  
  #define DELAY(x) [&amp;]{ return x; }
  
  #define MAKE_B(x1, x2) make_b(DELAY(x1), DELAY(x2))
  
  template &lt;class LAMBDA1, class LAMBDA2&gt;
  auto make_b(const LAMBDA1&amp; f1, const LAMBDA2&amp; f2) -&gt; B&lt;decltype(f1())&gt;
  {
    return B&lt;decltype(f1())&gt;( f1, f2 );
  }
  
  int main()
  {
    auto b1 = MAKE_B( A(), A() );
  }
  </code></pre>
  
  <p>If anyone knows how to achieve this more neatly I'd be quite interested to see it.</p>
  
  <p><b>Previous discussion:</b></p>
  
  <p>This somewhat follows on from the answers to the following questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/5865899/can-creation-of-composite-objects-from-temporaries-be-optimised-away">Can creation of composite objects from temporaries be optimised away?</a><br>
  <a href="https://stackoverflow.com/questions/5864624/avoiding-need-for-define-with-expression-templates">Avoiding need for #define with expression templates</a><br>
  <a href="https://stackoverflow.com/questions/5853860/eliminating-unnecessary-copies-when-building-composite-objects">Eliminating unnecessary copies when building composite objects</a>  </p>