CRTP的奇怪案例

时间:2019-07-05 03:30:20

标签: c++ crtp

任何想法如何在基类中访问派生类类型名?

template <class Derived>
class Base{
public:
  using TEMP = Derived::TEMP;
  virtual void implementation(TEMP a) = 0;
};

class Derived1: Base<Derived1>{
public:
  struct A{
    int a = 10;  
  };
  using TEMP = A;
  void implementation(TEMP a){
    std::cout << "Implementation Derived1 " << a.a << std::endl;
  }
};

class Derived2: Base<Derived2>{
public:
  struct B{
    int a = 20; 
  };
  using TEMP = B;
  void implementation(TEMP b){
    std::cout << "Implementation Derived2 " <<  b.a << std::endl;
  }
};

int main(){ 
  Derived1 d1;
  Derived1::TEMP t;
  d1.implementation(t);
}

0 个答案:

没有答案