title: Using rvalue-references for async callback
body: <p>I am working on a lib that will perform kind of scientific calculations.
  It is intended to be used with GUIs and therefore the calculations are performed asynchronously and the GUI (or whatever enqueues a task into the lib) will receive a callback on a previously specified <code>std::function</code>.
  Those callbacks shall always receive a small status response object, namely</p>
  
  <pre><code>struct StatusResponse {
      StatusResponse(int respC, std::string respS): responseCode(respC), 
      responseString(std::move(respS)) {};
      int responseCode;
      std::string responseString;
  };
  </code></pre>
  
  <p>Those callbacks are fire-and-forget, they don't return anything (void) and there is no need to wait for another action performed by the callback.
  Therefore also the StatusResponse objects are temporary.
  It seems like there are multiple ways to go now.
  My first idea was defining the callback function as follows</p>
  
  <pre><code>    template&lt;typename T0&gt;
  using Action = std::function&lt;void(std::shared_ptr&lt;T0&gt;)&gt;;
  </code></pre>
  
  <p>Calling it like</p>
  
  <pre><code>rdt::Action&lt;rdt::StatusResponse&gt; callback;
  
  std::shared_ptr&lt;rdt::StatusResponse&gt; response = 
  std::make_shared&lt;rdt::StatusResponse&gt;
  (rdt::errorcode::DEVICE_ID_OUT_OF_RANGE, std::string {"Device ID out 
  of Range!"});
  
  callback(response);
  return;
  </code></pre>
  
  <p>This seems quite inefficient and costly (use of <code>std::shared_ptr</code> for fire-and-forget, furthermore the program needs to return to the library function, just to return back...)
  Now I cooked up an alternative way:</p>
  
  <pre><code>void callback(std::unique_ptr&lt;StatusResponse&gt; response);
  
  std::unique_ptr&lt;StatusResponse&gt; response = 
  std::make_unique&lt;StatusResponse&gt;(0, "success!");
  
  callback(std::move(response));
  </code></pre>
  
  <p>This, however, still seems to be ineffective, because I am building a wrapper around an object that will be destroyed anyways.</p>
  
  <pre><code>void callback(StatusResponse&amp;&amp; response);
  
  StatusResponse response {0, "Success!"};
  callback(std::move(response));
  </code></pre>
  
  <p>is possible, or even</p>
  
  <pre><code>callback(StatusResponse {0, "Success!"});
  </code></pre>
  
  <p>So, now, I am quite unsure about those possibilities.</p>
  
  <p>First question: Is the <code>std::move</code> in the struct appropriate?</p>
  
  <p>Second question: Which solution will provide the best speed advantage? The callback will probably be called like</p>
  
  <pre><code>std::thread t(callback, StatusResponse {0, "Success!"});
  t.detach();
  </code></pre>
  
  <p>I just began learning about rvalue-references and smart pointers so any help is greatly appreciated!</p>
  
  <p>EDIT:
  I performed a test and SergeyA seems to be right.
  The rvalue reference way produces the following debugging output:</p>
  
  <pre><code>Constructor Called!
  Copy Constructor Called!
  Copy Constructor Called!
  Destructor Called!
  Destructor Called!
  Callback called!
  Called callback!
  Exiting caller!
  Exiting callback!
  Destructor Called!
  </code></pre>
  
  <p>Providing a move constructor leads to <code>Copy Constructor Called</code> being replaced by <code>Move Constructor Called</code></p>