title: Unable to move temporary object into the *this object
body: <p>While trying to create a Binary Search Tree (BST) using C++11 I hit a snag.
  I can't extract a properly created BST out of the function where it is created.
  The function reads data from a file (in this case just a number on each line) and builds a BST from those. The building part in the loop works correctly.
  The problem is I can't get the temporary object moved where I want it to be.</p>
  
  <p>Zooming in on the problem I think, see edit 3.</p>
  
  <p>More context:
  <code>BST&lt;T&gt;</code> is a class that derives publicly from <code>std::unique_ptr&lt;BSTknoop&lt;T&gt;&gt;</code><br>
  <code>BST&lt;T&gt;</code> also inherits the constructors of <code>unique_ptr&lt;BSTknoop&lt;T&gt;&gt;</code> with</p>
  
  <pre><code>template&lt;class T&gt;
  using BSTknoopptr=std::unique_ptr&lt;BSTknoop&lt;T&gt;&gt;; // alias template
  </code></pre>
  
  <p>and</p>
  
  <pre><code>using BSTknoopptr&lt;T&gt;::BSTknoopptr; // in the body of the BST&lt;T&gt; class declaration`
  </code></pre>
  
  <p>A <code>BSTknoop&lt;T&gt;</code> is a data structure with 3 fields
  one T object to hold the node data
  two <code>BST&lt;T&gt;</code> objects left and right (each child is a tree in its own right)</p>
  
  <p>the goal is to move my newly created BST into the calling object. The idea is that in main you can call <code>BST&lt;int&gt; tree; tree.lees(ifs);</code> (with <code>ifs</code> an open input filestream) and tree holds the properly filled BST afterwards.</p>
  
  <p>The function:</p>
  
  <pre><code>template&lt;class T&gt;
  istream&amp; BST&lt;T&gt;::lees(istream&amp; is){
      string lijn;
      T knoopwaarde;
      getline(is,lijn);
      knoopwaarde = stoi(lijn);
      BST&lt;T&gt; temptree(new BSTknoop&lt;T&gt;());
      temptree-&gt;sl = knoopwaarde;
  
      for(int i=1; i&lt;DATA_SET_LENGTH ; i++){
          getline(is,lijn);
          knoopwaarde=stoi(lijn);
          temptree.add(knoopwaarde);
          cout&lt;&lt;temptree;
      }
      this-&gt;swap(temptree);   /* This does not work */ 
      /* *this = move(temptree);   this does not work either*/
      return is;
  }
  </code></pre>
  
  <p>I have also tried to return a <code>BST&lt;T&gt;</code> from the function and moving that result in the main. That didn't work either.</p>
  
  <p>The program crashes at runtime with an unknown signal.</p>
  
  <p>Side note: I'm unsure as to why <code>BST&lt;T&gt; temptree(new BSTknoop&lt;T&gt;());</code> works in regard to the templating. The construction works because <code>BST&lt;T&gt;</code> inherits the constructors for <code>unique_ptr&lt;BSTknoop&lt;T&gt;&gt;</code></p>
  
  <p>edit 1: the declaration of the <code>BST&lt;T&gt;</code> class:</p>
  
  <pre><code>template &lt;class T&gt;
  class BST:public BSTknoopptr&lt;T&gt;{
      using BSTknoopptr&lt;T&gt;::BSTknoopptr;
  public:
      friend istream&amp; operator&gt;&gt;(istream&amp; is, BST&lt;T&gt;&amp; bb){
          return bb.lees(is);
      }
      friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const BST&lt;T&gt;&amp; bb){
          //return bb.schrijflevelorder(os);
          return bb.schrijfKnoop(os);
      }
      void add(const T&amp;);
      ostream&amp; schrijf(ostream&amp;);
      ostream&amp; schrijfKnoop(ostream&amp;) const;
      int aantalSleutels() const;
      istream&amp; lees(istream&amp;);   
      ostream&amp; schrijflevelorder(ostream&amp; os) const;
  private:
  };
  </code></pre>
  
  <p>the declaration of the<code>BSTknoop&lt;T&gt;</code> class:</p>
  
  <pre><code>template &lt;class T&gt;
  class BSTknoop{
      friend class BST&lt;T&gt;;
  public:
      BSTknoop() {}
      explicit BSTknoop(T _sl) : sl(_sl) {}
  private:
      T sl;
      BST&lt;T&gt; links,rechts;
  };
  </code></pre>
  
  <p>edit 2: I've written a move constructor and move assignment operator. I've made sure to retain the default constructor as well using <code>BST&lt;T&gt;()=default;</code> I'm confused though: the BST class doesn't have any members for which I would have to implement my own move constr / operator.<br>
  However, BST has inherited from <code>unique_ptr&lt;BSTknoop&lt;T&gt;&gt;</code>so implicitly, it must hold a member of that type. Suppose I want to keep the inheritance, is there any neat way to make this work?
  Is is also true that I can't (or perhaps shouldn't) implement a copy constr / operator as those are deleted for unique_ptr? </p>
  
  <pre><code>  template&lt;class T&gt;
  BST&lt;T&gt;::BST(BST&lt;T&gt;&amp;&amp; other){
      *this = move(other);
  }
  
  template&lt;class T&gt;
  BST&lt;T&gt;&amp; BST&lt;T&gt;::operator=(BST&lt;T&gt;&amp;&amp; other){
      cout&lt;&lt;"called move operator BST"&lt;&lt;endl;
      (*this).BSTknoopptr&lt;T&gt;::operator=(move(other));
      return *this;
  }
  </code></pre>
  
  <p>edit 3: with my own move constr / operator, the temptree no longer gets filled properly. Below is my code for add which is used to build the tree. If I omit my own move constr / operator, then this works. What I need is an operation that works like <code>this-&gt;get()-&gt;links = move(tmp)</code>. 
  types <code>this-&gt;get()-&gt;links =&gt; BST&lt;T&gt;</code>
       <code>tmp =&gt; BST&lt;T&gt;</code>
  How come this works? It does the operation I need, why can't I use something similar to do <code>*this = move(temptree)</code> by writing the operation myself.</p>
  
  <pre><code>template&lt;class T&gt;
  void BST&lt;T&gt;::add(const T&amp; value){
      if(value &lt;= this-&gt;get()-&gt;sl){
          if(this-&gt;get()-&gt;links != nullptr){
              this-&gt;get()-&gt;links.add(value);
          }
          else {
              BST&lt;T&gt; tmp(new BSTknoop&lt;T&gt;(value));
              this-&gt;get()-&gt;links = move(tmp);
          }
      }
      else{
          if(this-&gt;get()-&gt;rechts != nullptr){
              this-&gt;get()-&gt;rechts.add(value);
          }
          else{
              BST&lt;T&gt; tmp(new BSTknoop&lt;T&gt;(value));
              this-&gt;get()-&gt;rechts = move(tmp);
          }
      }
  }
  </code></pre>