title: Which "return" method is better for large data in C++/C++11?
body: <p>This question was triggered by confusion about RVO in C++11. </p>
  
  <p>I have two ways to "return" value: <strong>return by value</strong> and <strong>return via reference parameter</strong>. If I <strong>don't</strong> consider the performance, I prefer the first one. Since return by value is more natural and I can easily distinguish the input and the output. But, if I consider the efficiency when return large data. I can't decide, because in C++11, there is RVO.</p>
  
  <p>Here is my example code, these two codes do the same work:</p>
  
  <p><strong>return by value</strong></p>
  
  <pre><code>struct SolutionType
  {
      vector&lt;double&gt; X;
      vector&lt;double&gt; Y;
      SolutionType(int N) : X(N),Y(N) { }
  };
  
  SolutionType firstReturnMethod(const double input1,
                                 const double input2);
  {
      // Some work is here
  
      SolutionType tmp_solution(N); 
      // since the name is too long, I make alias.
      vector&lt;double&gt; &amp;x = tmp_solution.X;
      vector&lt;double&gt; &amp;y = tmp_solution.Y;
  
      for (...)
      {
      // some operation about x and y
      // after that these two vectors become very large
      }
  
      return tmp_solution;
  }
  </code></pre>
  
  <p><strong>return via reference parameter</strong></p>
  
  <pre><code>void secondReturnMethod(SolutionType&amp; solution,
                          const double input1,
                          const double input2);
  {
      // Some work is here        
  
      // since the name is too long, I make alias.
      vector&lt;double&gt; &amp;x = solution.X;
      vector&lt;double&gt; &amp;y = solution.Y;
  
      for (...)
      {
      // some operation about x and y
      // after that these two vectors become very large
      }
  }
  </code></pre>
  
  <p>Here are my questions:</p>
  
  <ol>
  <li>How can I <strong>ensure</strong> that RVO is happened in C++11?</li>
  <li>If we are sure that RVO is happened, in nowadays C++ programming, which "return" method do you <strong>recommend</strong>? Why?</li>
  <li>Why there are some library use the return via reference parameter, code style or historical reason?</li>
  </ol>
  
  <p><strong>UPDATE</strong>
  Thanks to these answers, I know the first method is better in most way.</p>
  
  <p>Here is some useful related links which help me understand this problem:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/37117815/how-to-return-large-data-efficiently-in-c11">How to return large data efficiently in C++11</a></li>
  <li><a href="https://stackoverflow.com/questions/3134831/in-c-is-it-still-bad-practice-to-return-a-vector-from-a-function">In C++, is it still bad practice to return a vector from a function?</a></li>
  <li><a href="https://web.archive.org/web/20130930101140/http://cpp-next.com/archive/2009/08/want-speed-pass-by-value" rel="nofollow noreferrer">Want Speed? Pass by Value.</a></li>
  </ol>