在阅读文章时,我遇到了以下语法:
template <typename T>
class MyTemplate
{
T* member;
T* method();
// ...
}
class MyClass : public MyTemplate<MyClass>
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我不完全理解如何MyClass从基于自身的模板继承.你能解释一下这是如何工作的吗?
这是有效的 C++ 吗?
template<class category>
class any_iterator : public any_iterator<void>
{
public:
typedef any_iterator<void> any_iter_void;
any_iterator() : any_iter_void() {}
};
template<>
class any_iterator<void>
{
public:
typedef any_iterator<void> any_iter_void;
any_iterator() {}
void foo() {};
};
int main() {
any_iterator<int> a;
a.foo();
}
Run Code Online (Sandbox Code Playgroud)
MSVC10 在没有错误/警告的情况下接受它\WALL,但gcc-4.5.1抱怨:
prog.cpp:3:5: 错误:不完整类型“class any_iterator”的无效使用
prog.cpp:2:11: 错误:“class any_iterator”
prog.cpp 的声明:在函数“int main()”中:
prog。 cpp:21:11: 错误: 'class any_iterator' 没有名为 'foo' 的成员
prog.cpp: 在构造函数 'any_iterator::any_iterator() [with category = int]':
prog.cpp:20:27: 实例化自这里
prog.cpp:7:44: 错误:类型“any_iterator”不是“any_iterator”的直接基础
有人可以引用标准显示是否应该或不应该编译吗?我认为这是 MSVC 中的一个错误。
作为说明,我知道正确的做法是声明类,专门化根,然后 …