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 8 6666
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/
"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)
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/
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
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/
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
"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)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Agent Mulder |
last post by:
Hi group,
I have a problem with partial template specialization. In the code
below I have a template struct Music with one method, play(),
and three kinds of music, Jazz, Funk and Bach. When I...
|
by: Dave |
last post by:
Hello all,
Consider this function template definition:
template<typename T>
void foo(T) {}
If foo is never called, this template will never be instantiated.
Now consider this explicit...
|
by: Nicolas Fleury |
last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment
welcomed (even about English mistakes).
PEP: XXX
Title: Specialization Syntax
Version: $Revision: 1.10 $...
|
by: Joseph Turian |
last post by:
I'm posting this question for a friend who lacks USENET access.
He and I were discussing this question and could not figure out the
solution.
Thank you for your help
Joseph
===
|
by: Marek Vondrak |
last post by:
Hello.
I have written the following program and am curious why it prints "1" "2".
What are the exact effects of explicitly providing function template
parameters at the call? Is the second...
|
by: Frank Millman |
last post by:
Hi all
I recently posted a question about subclassing. I did not explain my
full requirement very clearly, and my proposed solution was not pretty.
I will attempt to explain what I am trying to...
|
by: vilarneto |
last post by:
Hello everyone,
Today I started to use template specializations in a project and
suddenly faced a curious problem. Following is a complete example
that shows the situation:
----------
...
|
by: stephen.diverdi |
last post by:
Can anyone lend a hand on getting this particular template
specialization working? I've been trying to compile with g++ 4.1 and
VS 2005.
...
|
by: Barry |
last post by:
The following code compiles with VC8
but fails to compiles with Comeau online,
I locate the standard here:
An explicit specialization of any of the following:
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| | |