Re: Template Specialization, subclassing and overriding

From: Rob Williscroft (rtw_at_freenet.REMOVE.co.uk)
Date: 02/07/04


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/


Relevant Pages

  • Re: Compile issues with atlcom.h
    ... Here's what my declaration in atlcom.h looks ... template <class Base> ... class CComObject: public Base ... That is really strange. ...
    (microsoft.public.vc.atl)
  • Re: Deletion of a COM object in VC6.0
    ... > lauch2 wrote: ... template <class Base> ... class CComObjectCached: public Base ...
    (microsoft.public.dotnet.languages.vc)
  • Re: C2248: cannot access protected member
    ... class Base; ... class Derived: public Base { ... which is a fully-bound member function, ... Y can access the base implementation of protected member foo, ...
    (microsoft.public.vc.language)
  • Re: C2248: cannot access protected member
    ... class Base; ... class Derived: public Base { ... if Y is a subtype of X, then Y -> unit is a ... Y can access the base implementation of protected member foo, ...
    (microsoft.public.vc.language)
  • Re: C2248: cannot access protected member
    ... Alex Blekhman wrote: ... class Base; ... class Derived: public Base { ... and it could be dangerous sitting under them as they fly ...
    (microsoft.public.vc.language)