Re: Template Specialization, subclassing and overriding

From: John Carson (donaldquixote_at_datafast.net.au)
Date: 02/07/04


Date: Sun, 8 Feb 2004 00:23:14 +1100


"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:

class AClass
{};

class base
{
    protected:
    template<class X>
    void myfunc(X par1) { }
};

class derived : public base
{
    using base::myfunc;
    template<>
    void myfunc<AClass>(AClass par1) { }
};

int main()
{
    return 0;
}

The Comeau error message is:

"explicit specialization is not allowed in the current scope"

-- 
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Relevant Pages

  • Inheritance question - VB.NET 2005
    ... I have a problem with my class hierarchy at runtime and would appreciate ... I have a base class, let's call it "BaseClass", that has a public function ... write the common elements to the XML document and then each derived class ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Convert instance of derived class to base class
    ... public class BaseClass {... ... How can I serialize "bc" so that only the members of BaseClass are ... instance of my derived class to my base class -- rather than just casting ... the object as the base class, as .NET still sees the object as an instance ...
    (microsoft.public.dotnet.general)
  • Re: Qn on Restricting a public function overriding in base classes ?
    ... > I have a baseclass implementation of funXYZ, ... It doesn't matter if the function is public or not, a derived class ... >> can override any functions in the base class. ...
    (microsoft.public.vc.mfc)
  • Convert instance of derived class to base class
    ... public class BaseClass {... ... How can I serialize "bc" so that only the members of BaseClass are ... instance of my derived class to my base class -- rather than just casting ... the object as the base class, as .NET still sees the object as an instance ...
    (microsoft.public.dotnet.general)
  • Template Specialization, subclassing and overriding
    ... Can I specialize a template function in a subclass without overriding ... (the main template function is defined in a base class). ... derived class) then I can't use the base template without directly ...
    (comp.lang.cpp)