使用GCC中的基类模板的模板问题 [英] Problem with template using base class template in GCC

查看:68
本文介绍了使用GCC中的基类模板的模板问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下在类模板Tpl的

成员函数x()的声明中给出了错误,在Solaris下使用最新版本的GCC编译了



A级{};

B级{};


模板< typename Base>

class Tpl:protected Base

{

public:

template< typename AOrB>

int x(void){return(baseX< AOrB>()); }

};


class TheBase

{

public:


模板< typename AOrB>

int baseX(void);

};


模板<> ;

inline int TheBase :: baseX< A>(void){return(1); }


模板<>

inline int TheBase :: baseX< B>(void){return(2); }


Tpl< TheBase> aTpl;


但是如果我给''baseX''一个伪参数,其类型是模板的类型参数

,明确命名类型在

baseX调用中的参数,它确实编译:


class A {};

class B {};


模板< typename Base>

class Tpl:protected Base

{

public:

模板< typename AOrB>

int x(void){return(baseX(AOrB())); }

};


class TheBase

{

public:


模板< typename AOrB>

int baseX(AOrB aOrB);

};


模板< >

inline int TheBase :: baseX< A>(A a){return(1); }


模板<>

inline int TheBase :: baseX< B>(B b){return(2); }


Tpl< TheBase> aTpl;


标准中是否有一些规则需要这种行为?

或者这只是GCC编译器的一个问题吗?


让我感到惊讶的另一件事是GCC给了我一个错误,如果

我把''baseX''的部分专业化放在声明中

TheBase一类。


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX<AOrB>()); }
};

class TheBase
{
public:

template<typename AOrB>
int baseX(void);
};

template<>
inline int TheBase::baseX<A>(void) { return(1); }

template<>
inline int TheBase::baseX<B>(void) { return(2); }

Tpl<TheBase> aTpl;

But if I give ''baseX'' a dummy parameter whose type is the type parameter
to the template, making explicit naming of the type paramenter in the
baseX call unecessary, it does compile:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX(AOrB())); }
};

class TheBase
{
public:

template<typename AOrB>
int baseX(AOrB aOrB);
};

template<>
inline int TheBase::baseX<A>(A a) { return(1); }

template<>
inline int TheBase::baseX<B>(B b) { return(2); }

Tpl<TheBase> aTpl;

Is there some rule in the standard that requires this behavior?
Or is this just a problem with the GCC compiler?

Another thing that surprised me was that GCC gives me an error if
I put the partial specializations of ''baseX'' inside the declaration
of the class ''TheBase''.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

" Walt Karas" < WK **** @ yahoo.com>写了...
"Walt Karas" <wk****@yahoo.com> wrote...
以下在类模板Tpl的
成员函数x()的声明中给出了一个错误,在Solaris下使用最新版本的GCC进行编译:

A类} {};
类B {};

模板< typename Base>
类Tpl:protected Base
{
公开:
模板< typename AOrB>
int x(void){return(baseX< AOrB>()); }


我这样做是为了编译:


模板< typename AOrB>

inx x (){return this-> template baseX< AOrB>(); }

};

类TheBase
{
公开:

模板< typename AOrB>
int baseX(void);
};

模板<>
inline int TheBase :: baseX< A>(void){return(1);模板<>
inline int TheBase :: baseX< B>(void){return(2); }

Tpl< TheBase> aTpl;

但是如果我给''baseX''一个伪参数,其类型是模板的类型参数
,在
baseX中明确命名类型参数打电话给不必要的,它确实编译:

A类{};
类B {};

模板< typename Base>
类Tpl: protected Base
{
public:
template< typename AOrB>
int x(void){return(baseX(AOrB())); }
};

类公共:

模板< typename AOrB>
int baseX(AOrB aOrB );
};

模板<>
内联int TheBase :: baseX< A>(A a){return(1);模板<>
inline int TheBase :: baseX< B>(B b){return(2); }

Tpl< TheBase> aTpl;

标准中是否有一些需要此行为的规则?


是的,如果基类取决于模板类型,其范围不是为了解析依赖名称而搜索


或者这只是GCC编译器的一个问题吗?


我不这么认为。 Comeau也拒绝了第一个例子。

让我感到惊讶的另一件事是GCC给了我一个错误,如果我把''baseX''的部分特化列入声明中
TheBase的课程。
The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX<AOrB>()); }
I got it to compile by doing this:

template <typename AOrB>
inx x() { return this->template baseX<AOrB>(); }
};

class TheBase
{
public:

template<typename AOrB>
int baseX(void);
};

template<>
inline int TheBase::baseX<A>(void) { return(1); }

template<>
inline int TheBase::baseX<B>(void) { return(2); }

Tpl<TheBase> aTpl;

But if I give ''baseX'' a dummy parameter whose type is the type parameter
to the template, making explicit naming of the type paramenter in the
baseX call unecessary, it does compile:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX(AOrB())); }
};

class TheBase
{
public:

template<typename AOrB>
int baseX(AOrB aOrB);
};

template<>
inline int TheBase::baseX<A>(A a) { return(1); }

template<>
inline int TheBase::baseX<B>(B b) { return(2); }

Tpl<TheBase> aTpl;

Is there some rule in the standard that requires this behavior?
Yes, if the base class depends on the template type, its scope is not
searched to resolve a dependent name.
Or is this just a problem with the GCC compiler?
I don''t think so. Comeau also rejected the first example.
Another thing that surprised me was that GCC gives me an error if
I put the partial specializations of ''baseX'' inside the declaration
of the class ''TheBase''.




这也是禁止的。他们正考虑将其添加到下一个标准IIRC的




V



That''s also still prohibited. They are thinking of adding it to the
next Standard, IIRC.

V


" ; Victor Bazarov <五******** @ comAcast.net>在消息新闻中写道:< 81yXc.316605
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<81yXc.316605


a24.50596@attbi_s03> ...
a24.50596@attbi_s03>...
" Walt Karas" < WK **** @ yahoo.com>写了...
"Walt Karas" <wk****@yahoo.com> wrote...
以下在类模板Tpl的
成员函数x()的声明中给出了一个错误,在Solaris下使用最新版本的GCC进行编译:

A类} {};
类B {};

模板< typename Base>
类Tpl:protected Base
{
公开:
模板< typename AOrB>
int x(void){return(baseX< AOrB>()); }
The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX<AOrB>()); }



我这样做是为了编译:

模板< typename AOrB>
inx x(){return this->模板baseX< AOrB>(); }



I got it to compile by doing this:

template <typename AOrB>
inx x() { return this->template baseX<AOrB>(); }

};

类TheBase
{
公开:

模板< typename AOrB>
int baseX(void);
};

模板<>
inline int TheBase :: baseX< A>(void){return(1);模板<>
inline int TheBase :: baseX< B>(void){return(2); }

Tpl< TheBase> aTpl;

但是如果我给''baseX''一个伪参数,其类型是模板的类型参数
,在
baseX中明确命名类型参数打电话给不必要的,它确实编译:

A类{};
类B {};

模板< typename Base>
类Tpl: protected Base
{
public:
template< typename AOrB>
int x(void){return(baseX(AOrB())); }
};

类公共:

模板< typename AOrB>
int baseX(AOrB aOrB );
};

模板<>
内联int TheBase :: baseX< A>(A a){return(1);模板<>
inline int TheBase :: baseX< B>(B b){return(2); }

Tpl< TheBase> aTpl;

标准中是否有一些需要这种行为的规则?
};

class TheBase
{
public:

template<typename AOrB>
int baseX(void);
};

template<>
inline int TheBase::baseX<A>(void) { return(1); }

template<>
inline int TheBase::baseX<B>(void) { return(2); }

Tpl<TheBase> aTpl;

But if I give ''baseX'' a dummy parameter whose type is the type parameter
to the template, making explicit naming of the type paramenter in the
baseX call unecessary, it does compile:

class A { };
class B { };

template <typename Base>
class Tpl : protected Base
{
public:
template <typename AOrB>
int x(void) { return(baseX(AOrB())); }
};

class TheBase
{
public:

template<typename AOrB>
int baseX(AOrB aOrB);
};

template<>
inline int TheBase::baseX<A>(A a) { return(1); }

template<>
inline int TheBase::baseX<B>(B b) { return(2); }

Tpl<TheBase> aTpl;

Is there some rule in the standard that requires this behavior?



是的,如果基类依赖于模板类型,则其范围不是
搜索解决一个从属名称。



Yes, if the base class depends on the template type, its scope is not
searched to resolve a dependent name.

或者这只是GCC编译器的一个问题?
Or is this just a problem with the GCC compiler?



我不知道我们这么认为。 Comeau也拒绝了第一个例子。



I don''t think so. Comeau also rejected the first example.

让我感到惊讶的另一件事是GCC给了我一个错误,如果我把''baseX''的部分特化放在声明中
班级''TheBase''。
Another thing that surprised me was that GCC gives me an error if
I put the partial specializations of ''baseX'' inside the declaration
of the class ''TheBase''.



这也是禁止的。他们正在考虑将其添加到下一个标准IIRC。

V



That''s also still prohibited. They are thinking of adding it to the
next Standard, IIRC.

V




首先我想说它与MS 2003编译器编译好。


我相信它应该编译好,因为我看到

代码没什么问题。

但关于名称查找的观点,我已经阅读了基础

模板未被搜索但在这种情况下我们有规则

继承我们不是。您也可以尝试使用完全限定名称调用基本函数

,这对我来说也有用但是不需要
即:theBase :: baseX< T>(T b );。

尝试将继承公开,然后你应该可以直接从Tpl的实例调用

baseX。

我不喜欢我认为如果模仿一个类意味着我们失去了继承,那将是非常好的,所以我认为这是你的编译器的错误。


And你也谈到部分专业化,但这不是

部分专业化它是成员函数的完全专业化

baseX。我不确定所有规则,但我可以在

类中定义它们并且它工作正常。而且我不明白你为什么不能这样做?
这样做。我认为成员函数专业化通常是为了清晰度而在课堂上定义




HTH Paul



Firstly I''d like to say that it compiles ok with MS 2003 Compiler.

I believe it should compile ok as I can see nothing wrong with the
code.
But the point made about name lookup , I have read too that base
templates are not searched but in this case we have the rules of
inheritance do we not. You could also try to call the base funtion
with fully qualified name which also worked for me but was not
required i.e: theBase::baseX<T>(T b);.
Try making your inheritance public then you should be able to call
baseX directly from instances of Tpl.
I don''t think it would be very good if templating a class meant we
lost the inheritance, so I believe it''s a fault with your compiler.

And also you are speaking of partial specialization but this is not
partial specialization it''s full specialization of member function
baseX. I''m not sure on all the rules but I can define them within the
class and it works fine. And I don''t see why you shouldn''t be able too
do so. I think member function specializations are generally defined
out of class for clarity though.

HTH Paul


这篇关于使用GCC中的基类模板的模板问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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