c++ - 为什么我的 Curiously Recurring Template Pattern (CRTP) 不能引用派生类的 typedef?

标签 c++ templates gcc typedef crtp

<分区>

使用 curiously recurring template pattern 时,如果我试图从基类中引用属于派生类的 typedef,则无法引用它们; gcc 提示 no type named 'myType' in class Derived<...> .这似乎与使用 typedef、模板和奇怪的重复关系的其他方式不一致。

考虑:

/* crtp.cpp */

#include <iostream>
using namespace std;

// case 1. simple.

class Base {
public:
    typedef int a_t;

    a_t foo;
};

class Derived : public Base {
    a_t bar;
};

// case 2. template.

template<typename T>
class tBase {
public:
    typedef T b_t;
    T foo;
};

template <typename T>
class tDerived : public tBase<T> {
    typename tBase<T>::b_t bar;
};

// case 3. curiously recurring.

template <typename T, typename D>
class tCuriousBase {
public:
    typedef T c_t;
    c_t foo;
};

template <typename T>
class tCuriousDerived : public tCuriousBase<T,tCuriousDerived<T> > {
    typename tCuriousBase<T,tCuriousDerived<T> >::c_t bar;
};

// case 4. curiously recurring with member reference.

template <typename T, typename D>
class tCuriousMemberBase {
public:
    T foo;

    T get() {
        return static_cast<D*>(this)->bar;
    }
};

template <typename T>
class tCuriousMemberDerived : public tCuriousMemberBase<T, tCuriousMemberDerived<T> > {
public:
    T bar;

    tCuriousMemberDerived(T val) : bar(val) {}
};

// case 5. curiously recurring with typedef reference.

template <typename T, typename D>
class tCuriousTypeBase {
public:
    typedef T d_t;
    d_t foo;
    typename D::c_t baz;
};

template <typename T>
class tCuriousTypeDerived : public tCuriousTypeBase<T, tCuriousTypeDerived<T> > {
public:
    typedef T c_t;
    typename tCuriousTypeBase<T,tCuriousTypeDerived<T> >::d_t bar;
};

// entry point

int main(int argc, char **argv) {
    Derived::a_t one = 1;
    tDerived<double>::b_t two = 2;
    tCuriousDerived<double>::c_t three = 3;
    double four = tCuriousMemberDerived<double>(4).get();
    tCuriousTypeBase<double, tCuriousDerived<double> >::d_t five = 5;
    // tCuriousTypeDerived<double>::d_t six = 6; /* FAILS */

    cout << one   << endl;
    cout << two   << endl; 
    cout << three << endl;
    cout << four  << endl;
    cout << five  << endl;
    // cout << six << endl;
}

从(1)可以看出typedef确实是从base继承到derived的;可以通过派生类访问在基类中声明的 typedef。

从 (2) 可以看出,如果两个类都是模板,这仍然成立。

从 (3) 中,我们看到这种 typedef 继承在存在奇怪的重复模板关系的情况下仍然可以存在;我们在 three 的声明中通过派生类引用基类的 typedef .

从 (4) 中,我们看到派生类的成员变量 可以很容易地从基类访问。

从 (5) 中,我们看到我们可以访问在模板参数上定义的 typedef(这在我们声明 five 使用不通过继承相关的类型时有效)。

然而,一旦我们在 (6) 中使模板参数成为派生类,typedef 突然变得不可访问,即使它看起来与派生类中的任何成员变量一样定义良好。

这是为什么?

最佳答案

这就是“循环依赖”,或者说“不完整型”的另一个 self 。

模板“元编程”是“编程类型”,但它需要一定程度的语义才能正确实例化类型。

考虑这个类比:

class A; //forwarded

class B
{
   A* pa; //good: we know how wide a pointer is, no matter if we don't know yet anything about A.
   A a; // bad: we don't know how much space it requires
};

class A
{
  float m;
}; // now we know, but it's too late

这可以通过将 A 放在 B 之前来解决,但是如果 A 是

class A
{
   B m;
};

除了指针没有其他解决方案,因为递归将是无限的。 (A应该包含自己,而不是引用另一个拷贝)

现在,用同样的类比,让我们编写“类型”:

template<class D>
class A
{
   typedef typename D::inner_type my_type; //required D to be known when A<D> is instantiated...
   my_type m; // ... otherwise we cannot know how wide A<D> will be.
};

这个声明本身并不坏,直到我们开始将 D 定义为 ...

class D: //here we only know D exist
    public A<D> //but A size depende on D definition...
{
  ....
  typedef long double; inner_type
  ....
}; // ....we know only starting from here

所以,基本上,我们(还)不知道当时 A 有多宽,需要用它来创建 D。

打破这种“循环”的一种方法是在 CRT 循环之外使用一些“特征类”:

struct traits
{
   typedef long double inner_type;
   ....
};

template<class D, class Traits>
class A
{
  // this is easy: Traits does not depend itself on A
  typedef typename Traits::inner_type my_type;
  ....
};

template<class Traits>
class D: public A<D, Traits>
{
  typedef typename Traits::inner_type inner_type;
};

我们终于可以宣布

typedef D<traits> D_Inst;

换句话说,A::my_typeD::inner_type 之间的一致性由 traits::inner_type 确保,其定义是独立的。

关于c++ - 为什么我的 Curiously Recurring Template Pattern (CRTP) 不能引用派生类的 typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16098647/

相关文章:

C++ lambda 作为模板函数中的 std::function

c++ - gcc 未给出 Clang 错误 "attempted to construct a reference element in a tuple with an rvalue"

c - SSE:有条件地替换像素

c++ - QUADS 中的 OpenGL C++ 普通 segmentation ,迭代方法

c++ - 相机的 OpenGL 问题(?)

c++ - 调用专门的 ostream 运算符

Ruby - GCC 的依赖文件解析优化

c++ - 使用头文件而不是标准命名空间

c++ - iOS 上的 OpenCV 对象检测 (HOGDescriptor)

同时采用 const 和非常量类型的 C++ 模板类