问题描述
以下在类模板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一类。
[见有关的信息]
[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! ]
推荐答案
这也是禁止的。他们正考虑将其添加到下一个标准IIRC的
。
V
That''s also still prohibited. They are thinking of adding it to the
next Standard, IIRC.
V
我这样做是为了编译:
模板< 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>(); }
是的,如果基类依赖于模板类型,则其范围不是
搜索解决一个从属名称。
Yes, if the base class depends on the template type, its scope is not
searched to resolve a dependent name.
我不知道我们这么认为。 Comeau也拒绝了第一个例子。
I don''t think so. Comeau also rejected the first example.
这也是禁止的。他们正在考虑将其添加到下一个标准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中的基类模板的模板问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!