title: Cannot return std::unique_ptr from function without move
body: <p>I was updating some old code that uses <code>auto_ptr</code> to use <code>unique_ptr</code> instead. It was mostly a search and replace job, but I found I was getting a compilation error in one place where the code returns a <code>unique_ptr</code>.</p>
  
  <p>Here is an example that illustrates the issue:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include &lt;utility&gt;
  
  using namespace std;
  
  struct Foo {
      unique_ptr&lt;string&gt; us;
  
      Foo() {
          this-&gt;us = unique_ptr&lt;string&gt;(new string("hello"));
      }
  
      unique_ptr&lt;string&gt; bar() {
          return this-&gt;us;
      }
  
  };
  
  
  int main(int argc, const char **argv) {
  
      Foo foo;
      unique_ptr&lt;string&gt; s = foo.bar();
      cout &lt;&lt; *s &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>When I compile that, I get this:</p>
  
  <pre><code>t1.cpp: In member function ‘std::unique_ptr&lt;std::basic_string&lt;char&gt; &gt; Foo::bar()’:
  t1.cpp:17:16: error: use of deleted function ‘std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Dp&gt;&amp;) [with _Tp = std::basic_string&lt;char&gt;; _Dp = std::default_delete&lt;std::basic_string&lt;char&gt; &gt;]’
     return this-&gt;us;
                  ^~
  In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/memory:80:0,
                   from t1.cpp:4:
  /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/unique_ptr.h:388:7: note: declared here
         unique_ptr(const unique_ptr&amp;) = delete;
         ^~~~~~~~~~
  </code></pre>
  
  <p>If I alter the line with the error to specify a move like this:</p>
  
  <pre><code>        return move(this-&gt;us);
  </code></pre>
  
  <p>then it works.</p>
  
  <p>I have found multiple references indicating that a move should not be required - for example <a href="https://stackoverflow.com/questions/4316727/returning-unique-ptr-from-functions">this SO question</a>, and <a href="https://www.chromium.org/developers/smart-pointer-guidelines" rel="nofollow noreferrer">these guidelines</a> from the chromium project.</p>
  
  <p>My question is: why does the move need to be explicitly specified in this case? Is it related to fact that I am returning the value of an instance variable somehow?</p>
  
  <p>Apologies in advance if this is a dupe - I feel sure it would have been asked before, but I struggle to come up with search terms that find it.</p>