title: Why is copy constructor being invoked when I return a heap allocated object but not for stack allocated object?
body: <p>I have a <code>state</code> class which has a move assignment/constructor. The copy assingment/constructor are set to <code>delete</code>.</p>
  
  <p>I am confused why in the following function (which returns a <code>state</code> object) compiles and runs fine like this:</p>
  
  <pre><code>state propagator::PROPAGATE(date &amp; TargetDate)
  {
      jmethodID jmid_PROPAGATE = ENV-&gt;GetMethodID(this-&gt;jcls_PROPAGATOR, "propagate", "(path/to/date;)path/to/state;");
      jobject jobj_PROPAGATED_STATE = ENV-&gt;CallObjectMethod(this-&gt;jobj_PROPAGATOR, jmid_PROPAGATE, TargetDate.get_DATE_JOBJECT());
  
      state PROPAGATED_STATE(this-&gt;ENV);
      PROPAGATED_STATE.set_STATE_JOBJECT(jobj_PROPAGATED_STATE);
      return PROPAGATED_STATE;
  
      //state * PROPAGATED_STATE = new state(ENV);
      //PROPAGATED_STATE-&gt;set_STATE_JOBJECT(jobj_PROPAGATED_STATE);
      //return *PROPAGATED_STATE;
  }
  </code></pre>
  
  <p>but complains that copy constructor has been deleted when I try this:</p>
  
  <pre><code>state propagator::PROPAGATE(date &amp; TargetDate)
  {
      jmethodID jmid_PROPAGATE = ENV-&gt;GetMethodID(this-&gt;jcls_PROPAGATOR, "propagate", "(path/to/date;)path/to/state;");
      jobject jobj_PROPAGATED_STATE = ENV-&gt;CallObjectMethod(this-&gt;jobj_PROPAGATOR, jmid_PROPAGATE, TargetDate.get_DATE_JOBJECT());
  
      //state PROPAGATED_STATE(this-&gt;ENV);
      //PROPAGATED_STATE.set_STATE_JOBJECT(jobj_PROPAGATED_STATE);
      //return PROPAGATED_STATE;
  
      state * PROPAGATED_STATE = new state(ENV);
      PROPAGATED_STATE-&gt;set_STATE_JOBJECT(jobj_PROPAGATED_STATE);
      return *PROPAGATED_STATE;
  }
  </code></pre>
  
  <p>Compiler output:</p>
  
  <pre><code>error: use of deleted function ‘state::state(const state&amp;)’
  </code></pre>