C ++ - 循环依赖(在模板化基类中使用内部类型的子类)

时间:2012-08-28 05:49:01

标签: c++ templates circular-dependency crtp incomplete-type

我在模板化的类中遇到了循环依赖的问题。有一个代码示例:

template <typename T> struct A 
{
  typedef typename T::C D;
  //typename T::C c;
};

struct B : public A<B>
{
  struct C {};
};

当我尝试实例化B时,我收到编译器错误:'C'不是'B'的成员(MSVC)或无效使用不完整类型'struct B' (GCC) 更改样本以使其起作用的最佳方法是什么?

1 个答案:

答案 0 :(得分:6)

struct B_base { struct C {}; };
strucr B : A<B_base>, B_base { };