继承自专门的类?

Moo*_*uck 6 c++ inheritance templates template-specialization

这是有效的 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 中的一个错误。

作为说明,我知道正确的做法是声明类,专门化根,然后定义一般情况,这就是我将对我的代码做的事情,但我想知道这里哪个编译器有问题?

ild*_*arn 5

要从类型继承,该类型必须是完整的。稍微重新安排一下就可以解决问题:

template<class category>
class any_iterator;

template<>
class any_iterator<void>
{ 
public:
    typedef any_iterator<void> any_iter_void;

    any_iterator() { }
    void foo() { }
};

template<class category>
class any_iterator : public any_iterator<void>
{ 
public:
    typedef any_iterator<void> any_iter_void;

    any_iterator() : any_iter_void() { }
};

int main()
{
    any_iterator<int> a;
    a.foo();
}
Run Code Online (Sandbox Code Playgroud)

代币标准报价:

C++11,第 10/2 节:

基类型说明符表示的类型应该是一个不是不完整定义的类的类类型;这个类被称为被定义直接基类

§9.2/2:

}class-specifier结束时,类被认为是完全定义的对象类型(或完整类型)。