By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Template Specialization, subclassing and overriding

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Massimiliano Alberti wrote in
news:56*************************@posting.google.co m:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.200
Massimiliano Alberti wrote in
news:56*************************@posting.google.co m:
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)

Jul 22 '05 #3

P: n/a
John Carson wrote in news:40******@usenet.per.paradox.net.au:
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.200
Massimiliano Alberti wrote in
news:56*************************@posting.google.co m:
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/
Jul 22 '05 #4

P: n/a
On Sun, 08 Feb 2004 00:23:14 +1100, John Carson wrote:

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

Jul 22 '05 #5

P: n/a
Martijn Lievaart wrote in news:pan.2004.02.07.14.09.00.374228
@remove.this.part.rtij.nl:
On Sun, 08 Feb 2004 00:23:14 +1100, John Carson wrote:

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++.


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

P: n/a
On Sat, 07 Feb 2004 14:55:54 +0000, Rob Williscroft wrote:

Which is misleadin, specialization of functions is not possible in C++.
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

Jul 22 '05 #7

P: n/a
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.200
John Carson wrote in news:40******@usenet.per.paradox.net.au:
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.200
Massimiliano Alberti wrote in
news:56*************************@posting.google.co m:

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.


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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #8

P: n/a
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
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.