Template Specialization, subclassing and overriding

  • Thread starter Massimiliano Alberti
  • Start date
M

Massimiliano Alberti

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

void myfunc(AClass par1) { }

If I take away the template redefinition (the first block of the
derived class) then I can't use the base template without directly
referencing to it (I need to be able to do myfunc(something), not
baseClass::myfunc(something))
(oh... yes... if you want to know what compiler I'm using, I'm using
VC .NET 7.1)

--- bye
 
R

Rob Williscroft

Massimiliano Alberti wrote in
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.
 
J

John Carson

Rob Williscroft said:
Massimiliano Alberti wrote in


This isn't a specialization:


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

Martijn Lievaart

The Comeau error message is:

"explicit specialization is not allowed in the current scope"

Which is misleadin, specialization of functions is not possible in C++.

As a workaround, make it a class, but even then it is unclear to me what
you want to achieve. Maybe the following helps?

class AClass
{};

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

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

Obviously you now have to instatiate an object of struct A, so probably
struct A needs a constructor with a pointer to the object you wanted
myfunc to operate on in the first place.

HTH,
M4
 
R

Rob Williscroft

John Carson wrote in
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.
 
R

Rob Williscroft

Martijn Lievaart wrote in @remove.this.part.rtij.nl:
Which is misleadin, specialization of functions is not possible in C++.

template < typename T >
void f()
{
}

template <>
void f< int >()
{
}

Did you mean partial specialization ?

As a workaround, make it a class, but even then it is unclear to me
what you want to achieve. Maybe the following helps?

class AClass
{};

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

class derived : public base
{
using base::myfunc;

There is no base::myfunc, cut & paste error perhapse ?
template<>
struct A<AClass> {
void myfunc(AClass par1) { }
};

Here you are trying to specialize derived::A< >.

First, there is no class template derived::A<> so you can't
specialize it.

Second, if there were, you can't do it here, VC7.1 gives me exectly
the same error as it did for John's example.


Rob.
 
M

Martijn Lievaart

Did you mean partial specialization ?

Yes, sorry, I was way off.
Second, if there were, you can't do it here, VC7.1 gives me exectly
the same error as it did for John's example.

My bad, should think before posting. I think Rob gave the correct answer
to the OPs question. If not, OP, please restate.

M4
 
J

John Carson

Rob Williscroft said:
John Carson wrote in

Yep, one of us has missinterpreted the OP's problem.


The OP said "...pecialize a template function in a subclass..." which
is what you try to do here, and it can't be done.



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.



It is not clear what the OP wanted, but we seem to be agreed that
1. specialising a base class template function in a derived class is not
possible, and
2. overloading/overriding is better accomplished via a using declaration to
make the base class function visible in the derived class.
 
M

Massimiliano Alberti

I want to thank anyone that helped me. The idea of Ricky Lung worked
perfectly (using the "using" "trick").
And thank to all of you now I know that I wasn't looking for a
function-template-specialization (something that doesn't exist, as I have
discovered) but for an overload of a function-template :) (something that
all of you helped me find)

--- bye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top