Re: Template Specialization, subclassing and overriding
From: Rob Williscroft (rtw_at_freenet.REMOVE.co.uk)
Date: 02/07/04
- Next message: Martijn Lievaart: "Re: Template Specialization, subclassing and overriding"
- Previous message: Rolf Magnus: "Re: Overriding Methods"
- In reply to: John Carson: "Re: Template Specialization, subclassing and overriding"
- Next in thread: John Carson: "Re: Template Specialization, subclassing and overriding"
- Reply: John Carson: "Re: Template Specialization, subclassing and overriding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 07 Feb 2004 14:09:21 GMT
John Carson wrote in news:4024e6d1@usenet.per.paradox.net.au:
> "Rob Williscroft" <rtw@freenet.REMOVE.co.uk> wrote in message
> news:Xns948886A46F105ukcoREMOVEfreenetrtw@195.129.110.200
>> Massimiliano Alberti wrote in
>> news:567a3a9.0402070017.1a87a67c@posting.google.com:
>>
>>> Can I specialize a template function in a subclass without overriding
>>> it? (the main template function is defined in a base class).
>>> Now I'm doing something like that:
>>>
>>> (in base class)
>>> template<class X>
>>> void myfunc(X par1) { }
>>>
>>> (in derived class)
>>> template<class X>
>>> void myfunc(X par1) { baseClass::myfunc(par1); } // where baseClass
>>> is a typedef with the name of the baseclass
>>>
>>
>> This isn't a specialization:
>>
>>> void myfunc(AClass par1) { }
>>>
>>
>> One way or another you have to prevent the declaration in derived
>> from hiding the declaration in baseClass. The way you've done it
>> is fine, but you should also be able to hoist the baseClass version
>> (and also any overloads in baseClass) into the derived class with:
>>
>> using baseClass::myfunc;
>>
>> HTH.
>>
>> Rob.
>
> The following does not compile with either VC++ 7.1 or Comeau:
Yep, one of us has missinterpreted the OP's problem.
>
> class AClass
> {};
>
> class base
> {
> protected:
> template<class X>
> void myfunc(X par1) { }
> };
>
> class derived : public base
> {
> using base::myfunc;
The OP said "...pecialize a template function in a subclass..." which
is what you try to do here, and it can't be done.
> template<>
> void myfunc<AClass>(AClass par1) { }
> };
> The Comeau error message is:
>
> "explicit specialization is not allowed in the current scope"
>
>
>From the fragments of code the OP gave, I read that the OP was trying
to do this, which is overriding/overloading *not* specialization.
#include <iostream>
class AClass
{};
class base
{
public:
template<class X>
void myfunc(X par1) { std::cerr << "base::myfunc\n"; }
};
class derived : public base
{
public:
using base::myfunc;
void myfunc(AClass par1) { std::cerr << "defived::myfunc\n"; }
};
int main()
{
derived d;
d.myfunc( 0 );
d.myfunc( AClass() );
}
Rob.
-- http://www.victim-prime.dsl.pipex.com/
- Next message: Martijn Lievaart: "Re: Template Specialization, subclassing and overriding"
- Previous message: Rolf Magnus: "Re: Overriding Methods"
- In reply to: John Carson: "Re: Template Specialization, subclassing and overriding"
- Next in thread: John Carson: "Re: Template Specialization, subclassing and overriding"
- Reply: John Carson: "Re: Template Specialization, subclassing and overriding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|