title: Return a move-only object from member function
body: <p>I'm having a difficult time with understand + use move semantics in C++. I have an object <code>Variable</code> implements move constructor and move assignment but no copy constructor and assignment. Generally copying a <code>Variable</code> makes no sense and I want to prohibit copying explicitly.</p>
  
  <pre><code>class Variable {
  public:
      // ctod/dtor things
      Variable(Variable&amp;&amp;);
      Variable&amp; operator =(Variable&amp;&amp;);
      // ...
  };
  </code></pre>
  
  <p>The question is what is correct way of returning a <code>Variable</code> from a function?</p>
  
  <pre><code>Variable&amp; UserObject::giveMeYourVariable() {
      // Calculate parameters
      Variable v(/* Some parameters */);
      return v; // &lt;=== warning: reference to local variable 'v' returned
  }
  </code></pre>
  
  <p>In another code:</p>
  
  <pre><code>UserObject x* = new UserObject;
  Variable v = std::move(x-&gt;giveMeYourVariable())
  </code></pre>
  
  <p>Above code compiles with no error but a warning about returning a reference to local variable. Does this code leaks memory or cause undefined behavior or return a deleted reference? What am doing wrong?</p>
  
  <p><strong>Update</strong><br>
  Return by value causes error while initializing a reference type (inside a code that is generated by a parser-generator):</p>
  
  <pre><code>Variable&amp; tmp (this-&gt;a_function()); &lt;&lt;== error
  </code></pre>
  
  <p>The error says:</p>
  
  <pre><code>error: invalid initialization of non-const reference of type 'Variable&amp;' \\
  from an rvalue of type 'Variable'
  </code></pre>
  
  <p><strong>Update 2</strong><br>
  This issue is <a href="http://codesynthesis.com/pipermail/xsd-users/2013-July/003996.html" rel="nofollow">reported</a> in XSD mailing list and will be <a href="http://codesynthesis.com/pipermail/xsd-users/2013-July/004000.html" rel="nofollow">resolved</a> in next release.</p>