奇怪的循环模板

Curiously Recurring Templates?

本文关键字:循环      更新时间:2023-10-16

我有一个问题,好奇循环模板可以很好地帮助,但我甚至不能通过一个简单的测试。

template<typename T, int _size, typename OutterT>
class Foo {
};
template<typename T>
class Bar : public Foo<T, 2, Bar> {};
//typedef Bar<float> Vec2f;

int main()
{
    return 0;
}

这会导致错误

foo.cpp:7: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, int _size, class OuterT> class Foo’
foo.cpp:7: error:   expected a type, got ‘Bar’

我错过了什么

template<typename T, int _size, typename OutterT>
class Foo {
};
template<typename T>
class Bar : public Foo<T, 2, Bar<T> > {};
//                              ^^^
Bar<float> x;

由于Bar是一个模板,您必须提供template参数来将其实例化为一个类。