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::myfu nc(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(somethin g), not
baseClass::myfu nc(something))
(oh... yes... if you want to know what compiler I'm using, I'm using
VC .NET 7.1)
--- bye 8 6729
Massimiliano Alberti wrote in
news:56******** *************** **@posting.goog le.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::myfu nc(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::myfu nc;
HTH.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
"Rob Williscroft" <rt*@freenet.RE MOVE.co.uk> wrote in message
news:Xn******** *************** ***********@195 .129.110.200 Massimiliano Alberti wrote in news:56******** *************** **@posting.goog le.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::myfu nc(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::myfu nc;
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******@u senet.per.parad ox.net.au: "Rob Williscroft" <rt*@freenet.RE MOVE.co.uk> wrote in message news:Xn******** *************** ***********@195 .129.110.200 Massimiliano Alberti wrote in news:56******** *************** **@posting.goog le.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::myfu nc(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::myfu nc;
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 "...peciali ze 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::myfun c\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.0 2.07.14.09.00.3 74228
@remove.this.pa rt.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.RE MOVE.co.uk> wrote in message
news:Xn******** *************** ***********@195 .129.110.200 John Carson wrote in news:40******@u senet.per.parad ox.net.au:
"Rob Williscroft" <rt*@freenet.RE MOVE.co.uk> wrote in message news:Xn******** *************** ***********@195 .129.110.200 Massimiliano Alberti wrote in news:56******** *************** **@posting.goog le.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::myfu nc(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::myfu nc;
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 "...peciali ze 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::myfun c\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 specialize
Music<Bach>, I expect that the original play() method is available
in the specialization, but it is not. How can I fix this?
-X
|
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 instantiation of foo:
|
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 $
Last-Modified: $Date: 2003/09/22 04:51:49 $
Author: Nicolas Fleury <nidoizo at gmail.com>
Status: Draft
Type: Standards Track
|
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 assign() function really a
specialization of the first assign() or is it an assign() overload?
Thank you.
-- Marek
| |
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 do more fully, and
describe a possible solution. It is still not pretty, so I would
appreciate any comments.
I have a base class (ClassA), which is an abstract class. Most of the
methods and attributes are common to all subclasses, so there is...
|
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:
----------
class A {
};
|
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.
//------------------------------------------------------------------
// my regular glass
class A { };
// my templated class
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |