why curiously recurring template pattern (CRTP) works
我遇到了很多解释什么是CRTP,但没有解释为什么它起作用。
The Microsoft Implementation of CRTP in ATL was independently discovered, also in 1995 by Jan Falkin who accidentally derived a base class from a derived class. Christian Beaumont, first saw Jan's code and initially thought it couldn't possibly compile in the Microsoft compiler available at the time. Following this revelation that it did indeed did work, Christian based the entire ATL and WTL design on this mistake.
例如,
1 2 3 4 5 6 7 8 9 10 | template< typename T > class Base { ... }; class Derived : public Base< Derived > { ... }; |
我理解为什么以及何时可以使用它。但我想知道编译器是如何以这种方式工作的。因为在我看来,由于无休止的递归,它不应该工作:类派生继承自base
递归定义的类型并不少见:链表也是递归的。它之所以有效,是因为在循环的某一点上,您不需要完整的类型,只需要知道它的名称。
1 2 3 4 | struct LinkedNode { int data; LinkedNode *next; // Look ma, no problem }; |
就CRTP而言,这一点如下:
1 | Base<Derived> |
为
1 2 3 4 5 6 | template <class> struct Foo { }; struct Undefined; Foo<Undefined> myFoo; |
因此,只要
1 | public Base< Derived > |
这里,