title: How can I convert std::make_unique<derived>() to std::unique_ptr<base>
body: <p>I'm trying to build a factory method with self-registration based on (<a href="https://www.codetg.com/article/7r1QnR43bm3ZogBJ.html" rel="nofollow noreferrer">https://www.codetg.com/article/7r1QnR43bm3ZogBJ.html</a>), which registers logic operations. But I can't figure out how to convert std::make_unique to std::make_unique. I'm always getting the same error:</p>
  
  <pre><code>return': cannot convert from 'std::unique_ptr&lt;T1,std::default_delete&lt;_Ty&gt;&gt;' to 'std::unique_ptr&lt;LogicOperation,std::default_delete&lt;_Ty&gt;&gt;
  </code></pre>
  
  <p>I'm still a noob on the subject of  unique pointer but I've read on cppreference.com </p>
  
  <pre><code>If T is a derived class of some base B, then std::unique_ptr&lt;T&gt; is implicitly convertible to std::unique_ptr&lt;B&gt;. 
  The default deleter of the resulting std::unique_ptr&lt;B&gt; will use operator delete for B, 
  leading to undefined behavior unless the destructor of B is virtual.
  </code></pre>
  
  <p>I have tried instead of making a lambda function,to use std::move() as shown in other examples on stackoverflow. But that does not same to work either.</p>
  
  <p>main</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int main()
  {
      Signal a;
      Signal b;
      a.setState(1);
      b.setState(0);
      std::unique_ptr&lt;LogicOperation&gt; logic = LogicOperationFactory::Create("AND");
      bool x[2] = { a.getState(), b.getState() };
      bool y = logic-&gt;operation(x, 2); // do and operation
  }
  
  </code></pre>
  
  <p>LogicOperation.h</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class LogicOperation
  {
  public:
      LogicOperation() = default;
      virtual ~LogicOperation() = default;
  public:
      virtual bool operation(bool*, uint8_t count) = 0;
  };
  </code></pre>
  
  <p>LogicOperationFactory.h:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>        using TCreateMethod = std::function&lt;std::unique_ptr&lt;LogicOperation&gt;()&gt;;
      template&lt;class T1&gt;
      static bool Register(const std::string name)
      {
          std::map&lt;std::string, TCreateMethod&gt;::iterator it;
          it = s_methods.find(name);
  
          if (it != s_methods.end())
              return false;
  
          s_methods[name] = []() -&gt; std::unique_ptr&lt;LogicOperation&gt; {
              // Constructs an object of type T and wraps it in a std::unique_ptr
              return std::make_unique&lt;T1&gt;(); // use default constructor
          };
  
          return true;
      }
  </code></pre>
  
  <p>LogicAndOperation.cpp</p>
  
  <pre><code>class LogicAndOperation :
      public virtual LogicOperation
  {
  public:
      LogicAndOperation() = default;
      virtual ~LogicAndOperation() = default;
  
      bool operation(bool* signals, uint8_t count) override;
  private:
      static bool s_registered;
  };
  
  bool LogicAndOperation::s_registered =
  LogicOperationFactory::Register&lt;LogicAndOperation&gt;("AND");
  </code></pre>
  
  <p>Can someone please explain to me, how I can make std::unique_ptr from a derived class (LogicAndOperation)?</p>