c++ - 奇怪的重复模板?

标签 c++ templates crtp f-bounded-polymorphism

我有一个问题,Curiously Recurring Templates 可以很好地帮助我,但我什至无法通过简单的测试。

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’

我错过了什么。

使用 g++ 4.2.1 编译

最佳答案

template<typename T, int _size, typename OutterT>
class Foo {

};

template<typename T>
class Bar : public Foo<T, 2, Bar<T> > {};
//                              ^^^

Bar<float> x;

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

关于c++ - 奇怪的重复模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5584088/

相关文章:

C++ - 从基类继承,成员函数作为模板参数

c++ - 未解析的模板参数

html - 在 "HEAD"页面的 "HTML"元素之外声明 CSS 样式?

c++ - 模板函数错误

c++ - enable_if 结构体定义和默认模板参数

c++ - 调用父构造函数时如何避免模板参数上的样板

c++ - 使用派生类的静态 constexpr 数据成员初始化基类的静态 constexpr 数据成员

c++ - 包含 unordered_map 的结构的大小(以字节为单位)

c++ - 指向 const 对象的指针自动转换为指向对象的指针

c++ - 位交换和指针