继承自专门的自我? [英] inheriting from class of specialized self?

查看:141
本文介绍了继承自专门的自我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有效的C ++吗?

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

any_iterator():any_iter_void(){}
};
模板<>
class any_iterator< void>
{
public:
typedef any_iterator< void> any_iter_void;

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

int main(){
any_iterator< int>一个;
a.foo(); MSVC10接受它,但在 \\上没有错误/警告。


\\ WALL
,但 gcc-4.5.1 抱怨:


prog.cpp:3:5:错误:无效使用不完整的类'any_iterator'

prog.cpp:2:11:error :'any_iterator'的声明

prog.cpp:在函数'int main()'中:

prog.cpp:21:11:error:'class any_iterator'has no成员名为'foo'

prog.cpp:在构造函数'any_iterator :: any_iterator()[with category = int]'中:

prog.cpp:20:27:这里

prog.cpp:7:44:error:type'any_iterator'不是'any_iterator'的直接基础


有人可以引用标准显示,如果这应该或不应该编译?我认为这是MSVC中的一个错误。



注意,我知道正确的做法是声明类,专门化根, / em>定义一般情况,这是我将对我的代码,但我想知道哪个编译器是错误的。

解决方案

要继承类型,该类型必须是完整的。有点重排解决问题:

 模板< class category> 
class any_iterator;

模板<>
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.foo();
}






令牌标准报价: / p>

C ++ 11,§10/ 2:


一个 base-type-specifier 应该是一个不是不完全定义类的类类型;这个类被定义的类称为直接基类


§9.2/ 2: / p>


类在关闭}



Is this valid 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();
}

MSVC10 accepts it with no errors/warnings on \WALL, but gcc-4.5.1 complains:

prog.cpp:3:5: error: invalid use of incomplete type 'class any_iterator'
prog.cpp:2:11: error: declaration of 'class any_iterator'
prog.cpp: In function 'int main()':
prog.cpp:21:11: error: 'class any_iterator' has no member named 'foo'
prog.cpp: In constructor 'any_iterator::any_iterator() [with category = int]':
prog.cpp:20:27: instantiated from here
prog.cpp:7:44: error: type 'any_iterator' is not a direct base of 'any_iterator'

Can someone quote the standard showing if this should or should not compile? I think this is a bug in MSVC.

As a note, I know the correct thing to do is to declare the class, specialize the root, then define the general case, and that's what I'll do to my code, but I was wondering which compiler is wrong here?

解决方案

To inherit from a type, that type must be complete. A little rearranging solves things:

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();
}


Token standard quotes:

C++11, §10/2:

The type denoted by a base-type-specifier shall be a class type that is not an incompletely defined class; this class is called a direct base class for the class being defined.

§9.2/2:

A class is considered a completely-defined object type (or complete type) at the closing } of the class-specifier.

这篇关于继承自专门的自我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆