title: access and move unique_ptr in a function call
body: <p>I have a segment similar to the following.</p>
  
  <pre><code>struct derive : base{
      derive(unique_ptr ptr): base{func(ptr-&gt;some_data), std::move(ptr)}{}
  };
  </code></pre>
  
  <p>In theory, it should work. But since the compiler (vs2015) does not strictly follow the standard, the order of <code>func(ptr-&gt;some_data), std::move(ptr)</code> is undefined, i.e. <code>ptr</code> may be moved before accessed.</p>
  
  <p>So my problem is how to make this segment work as expected?</p>
  
  <p><strong>Complete code like this:</strong></p>
  
  <pre><code>#include &lt;memory&gt;
  
  struct base {
      virtual ~base() = 0 {}
  
  protected:
      base(std::unique_ptr&lt;base&gt; new_state) :
          previous_state{ std::move(new_state) } {}
  private:
      std::unique_ptr&lt;base&gt; previous_state;
  };
  
  struct derive_base : base {
      int get_a() const noexcept {
          return a;
      }
  protected:
      derive_base(int const new_a, std::unique_ptr&lt;base&gt; new_state) :
          base{ std::move(new_state) }, a{ new_a } {}
  private:
      int a;
  };
  
  struct final_state : derive_base {
      final_state(std::unique_ptr&lt;base&gt; new_state) :
          derive_base{ dynamic_cast&lt;derive_base&amp;&gt;(*new_state).get_a(), std::move(new_state) } {}
  };
  </code></pre>