title: Overload rvalue and lvalue reference for template deduced type with return value and its implementation
body: <p>There are a lot of similar questions here on SO (e.g.: <a href="https://stackoverflow.com/questions/46935605/how-to-get-different-overloads-for-rvalue-and-lvalue-references-with-a-template">How to get different overloads for rvalue and lvalue references with a template-deduced type?</a>), but not exactly this one. In particular, no questions are concered with value returning functions. Furthermore, I am not sure (euphemestically spoken) whether I understood the answers correctly.</p>
  <hr />
  <p>I want to provide two versions of a function, which either return a new object or change the passed object (and return the changed object -- for consistency). For simplicity I present my code using a unique like algorithm.</p>
  <p>User code</p>
  <pre><code>auto vec = std::vector&lt;int&gt;{1,3,5,2,4};
  auto vec1 = tt::unique( vec );  
    // does not change vec and returns new vector
    // vec1 should be {1,2,3,4,5}
    // vec should be {1,3,5,2,4}
  
  auto vec2 = tt::unique( std::move(vec) );  
    // does change vec and returns new vector vec2
    // vec2 should be {1,2,3,4,5}
    // vec should be empty
  </code></pre>
  <p>Implementation:</p>
  <pre><code>namespace tt {
      template&lt; typename T &gt; T unique( T &amp;&amp; vec ) {
          std::sort( vec.begin(), vec.end() );
              vec.erase(
              std::unique( vec.begin(), vec.end()),
              vec.end()
          );
          return std::move( vec );
      }
      template&lt; typename T &gt; T unique( T &amp; vec ) {
          return unique( T(vec) );
      }
  }
  </code></pre>
  <p>To avoid code duplication the lvalue reference version calls the rvalue reference version.</p>
  <p>My main questions:</p>
  <ul>
  <li>Is this correct?</li>
  <li>I think by calling the rvalue reference version from the lvalue reference version, I do one extra move.</li>
  <li>Apart from this extra move, do I make any other unnecessary constructions?</li>
  <li>Given the extra move, for very simple algorithms (oneliners), is it more efficient to duplicate the code? E.g. a <em>sort</em> above function I would implement as
  <pre><code>template&lt; typename T &gt; T sort( T &amp; array ) {
      T array_ = array;
      std::sort( array_.begin(), array_.end() );
      return array_;
  }
  </code></pre>
  Is this efficient/correct code?</li>
  </ul>
  <p>Extra questions:</p>
  <ul>
  <li><p>Why is this overload never used and why does is compile at all, given that c is const.</p>
  <pre><code>template&lt; typename T &gt;
  T unique( const T &amp;&amp; c ) {
      std::cout &lt;&lt; &quot;Rvalue reference unique wrapper!\n&quot;;
      return unique( T(c) );
  }
  </code></pre>
  </li>
  <li><p>Actually I would like to have const in the lvalue reference signature, i.e. something like
  <code>template&lt;typename T&gt; T unique( const T &amp; vec );</code>. But, as I read, since the first is a <em>forwarding reference</em>, this does not work.</p>
  <p>Thus, what do I have to do to get const correctness here (My question is about C++14, but I am also interrested in C++17 answers).</p>
  </li>
  </ul>