std :: unique_ptr和模板

use*_*174 0 c++ c++11

我有一个使用CRTP的场景.下面的伪代码:

template <typename T>
class Base {} 

class Derived1 : Base < Derived1 >  {}

class Derived2 : Base < Derived2 > {} 
Run Code Online (Sandbox Code Playgroud)

一切正常,除非我介绍unique_ptr循环.我希望代码中的unique_ptrto Base和其他地方使用它来获取a Derived1Derived2指针的所有权.

// declaration - this is the problem - wont compile.
std::unique_ptr<Base> base_unique_ptr;

// cpp , elsewhere.
base_unique_ptr.reset(new Derived1());
Run Code Online (Sandbox Code Playgroud)

要么

base_unique_ptr.reset(new Derived2());
Run Code Online (Sandbox Code Playgroud)

我有麻烦吗?我不想改变现有代码的使用unique_ptr.

Mag*_*nRa 6

Base不是一个合适的类型.您需要为其指定模板参数Base.我假设你想要base_unique_ptrDerived1和Derived2,这是不可能的,因为它们有不同的基类. Base<Derived1>并且Base<Derived2>是不同的类型.