title: Perfectly capturing a perfect forwarder (universal reference) in a lambda
body: <p>So I have a perfect forwarder, and I want to appropriately capture it in a lambda, such that R-values are copied in, and L-values are captured by reference.  However simply using std::forward doesn't do the job, as evidenced by this code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  class testClass
  {
  public:
     testClass() = default;
     testClass( const testClass &amp; other ) { std::cout &lt;&lt; "COPY C" &lt;&lt; std::endl; }
     testClass &amp; operator=(const testClass &amp; other ) { std::cout &lt;&lt; "COPY A" &lt;&lt; std::endl; }
  };
  
  template&lt; class T&gt;
  void testFunc(T &amp;&amp; t)
     { [test = std::forward&lt;T&gt;(t)](){}(); }
  
  int main()
  {
     testClass x;
     std::cout &lt;&lt; "PLEASE NO COPY" &lt;&lt; std::endl;
     testFunc(x);
     std::cout &lt;&lt; "DONE" &lt;&lt; std::endl;
  
     std::cout &lt;&lt; "COPY HERE" &lt;&lt; std::endl;
     testFunc(testClass());
     std::cout &lt;&lt; "DONE" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Compiling this with </p>
  
  <pre><code>g++ -std=c++14 main.cpp
  </code></pre>
  
  <p>Produces the output</p>
  
  <pre><code>PLEASE NO COPY
  COPY C
  DONE
  COPY HERE
  COPY C
  DONE
  </code></pre>
  
  <p>In a perfect world, I would like to only have the "COPY C" appear in the rvalue case, not the lvalue case.</p>
  
  <p>My work around would be to use a helper function overloaded for L- and R- values, but I was wondering if there was a better way.</p>
  
  <p>Cheers!</p>