title: How to avoid the "pessimizing-move" warning of NRVO?
body: <pre><code>#include &lt;string&gt;
  
  std::string f()
  {
      std::string s;
      return std::move(s);
  }
  
  int main()
  {
      f();
  }
  </code></pre>
  
  <p><code>g++ -Wall z.cpp</code> gives a warning as follows:</p>
  
  <pre><code>z.cpp: In function ‘std::string f()’:
  z.cpp:6:21: warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
      6 |     return std::move(s);
        |            ~~~~~~~~~^~~
  z.cpp:6:21: note: remove ‘std::move’ call
  </code></pre>
  
  <p>I know if I change <code>return std::move(s);</code> to <code>return s;</code>, the warning will be avoided. However, according to the <a href="https://en.cppreference.com/w/cpp/language/copy_elision" rel="nofollow noreferrer">C++ standard</a>, <code>NRVO</code>, say in this case, is not guaranteed. If I write <code>return s;</code>, I feel uncertain whether <code>NRVO</code> will be executed.</p>
  
  <p>How to ease the feel of uncertainty?</p>