title: SmartPointer in C++ using template
body: <p>I am trying to implement a smart pointer (basically unique pointer) in C++ using templates for my understanding. </p>
  
  <p>This is what I have coded</p>
  
  <pre><code>using namespace std;
  
  template&lt;typename T&gt;
  class smartPointer
  {
      private:
          T *mPtr;
      public:
          smartPointer(T* init=nullptr):mPtr(init)
          {
              cout&lt;&lt;"Inside ctr"&lt;&lt;endl;
          }
  
  
          //silence the default ctr
          smartPointer() = delete;
  
          //disable copy ctr and copy assignment
          smartPointer  (const smartPointer&amp; other) = delete;
          smartPointer&amp; operator=(const smartPointer&amp; other) = delete;
  
          //implement move ctr and move assignment
          smartPointer  (smartPointer&amp;&amp; other)
          {
              cout&lt;&lt;"Inside move ctr "&lt;&lt;endl;
              mPtr = other.mPtr;
              other.mPtr = nullptr;
          }
          smartPointer&amp; operator=(smartPointer&amp;&amp; other)
          {
  
              cout&lt;&lt;"Inside move = before "&lt;&lt;endl;
              if(&amp;other != this)
              {
                  mPtr = other.mPtr;
                  other.mPtr = nullptr;
  
                  cout&lt;&lt;"Inside move = after "&lt;&lt;endl;
              }
          return *this;
          }
  
          //deference
          T&amp; operator*()
          {
              return *mPtr;
          }
  
          //arrow  access operator
          T* operator-&gt;()
          {   
              return mPtr;
          }
  
          ~smartPointer()
          {   
              cout&lt;&lt;"Inside ~dtr"&lt;&lt;endl;
              if(mPtr != nullptr)
              delete mPtr;
          }
  
  };
  
  
  
  int main()
  {
  
       smartPointer&lt;int&gt; sptr(new int);
  
       // Even smartPointer&lt;int&gt; sptr(new int[20]); too works
  
       *sptr = 10;
       cout&lt;&lt;"Value pointed by the pointer  is "&lt;&lt;*sptr&lt;&lt;endl;     
  
      smartPointer&lt;int&gt; sptr2(new int);
  
  //  sptr2 = sptr; // complains as expected
      sptr2 = move(sptr); // works well
  
      /*
      How to 
  
      smartPointer&lt;int[]&gt;  sptr(new int[20]); 
      */
  
      return 0;
  }
  </code></pre>
  
  <p>How to make this smart pointer to work for smartPointer ?</p>
  
  <p>The build command I give is 
  g++ -std=c++11 -smartPointer.cpp -o smartPointer</p>