473,748 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Partial Template Specialization

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

struct Jazz{};
struct Funk{};
struct Bach{};
template<struct A>struct Music{void play(){}};
struct Music<Bach>{}; //partial template specialization
int main()
{
Music<Jazz>().p lay(); //OK
Music<Funk>().p lay(); //OK
Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
return 0;
}

Jul 19 '05 #1
8 7688

"Agent Mulder" <mb************ *******@home.nl > wrote in message
news:bo******** **@news4.tilbu1 .nb.home.nl...
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

struct Jazz{};
struct Funk{};
struct Bach{};
template<struct A>struct Music{void play(){}};
struct Music<Bach>{}; //partial template specialization
int main()
{
Music<Jazz>().p lay(); //OK
Music<Funk>().p lay(); //OK
Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
return 0;
}


I'm sure others will be able to give a more complete answer than I, but in
looking at your code, your specialization for Bach does indeed not have a
play() member function. In fact, it doesn't have any member functions or
data members - it's an empty class! I believe I'm correct in saying that
your partial specialization will have only what you explicitly put in it.
I'll rely on others to correct me if I'm wrong, but I think what I said is
correct...

Also, template<struct A> should be either template<typena me A> or
template<class A> (these two are equivalent).
Jul 19 '05 #2
Agent Mulder wrote:
...
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?
...
struct Jazz{};
struct Funk{};
struct Bach{};
template<struct A>struct Music{void play(){}};
struct Music<Bach>{}; //partial template specialization
This is not partial specialization. This is explicit specialization. And
the correct syntax is a s follows

template<> struct Music<Bach> {};
int main()
{
Music<Jazz>().p lay(); //OK
Music<Funk>().p lay(); //OK
Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
return 0;
}
...


Explicit (or partial) specialization of a template is an independently
defined template. You defined it as a template class with no explicit
members. That's what you get when you instantiate the specialized
version - a class with no explicit members. It doesn't have method
'play()', hence the error.

If you want to have an explicit specialization of the entire
'Music<Bach>' and still have method 'play' in it, you have no other
choice but to declare and define this method in 'Music<Bach>' explicitly.

template<> struct Music<Bach> { void play( /* whatever */ ); };

On the other hand, if you just want to explicitly specialize the method
'play' for each kind of music, there is no need to explicitly specialize
the entire template 'Music'

struct Jazz {};
struct Funk {};
struct Bach {};

template<struct A> struct Music { void play(){} };

// Explicit specializations for method 'play()'
template<> void Music<Jazz>::pl ay() { /* whatever 1 */ }
template<> void Music<Funk>::pl ay() { /* whatever 2 */ }
template<> void Music<Bach>::pl ay() { /* whatever 3 */ }

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3
Agent Mulder wrote:
...
template<struct A>struct Music{void play(){}};
...


BTW, something I haven't noticed right away: you cannot use keyword
'struct' to declare template parameters of a template. The only keywords
that can be used for this purpose are 'typename' and 'class'.

By using keyword 'struct' you are actually making an attempt to declare
a non-type parameter of type 'struct A'. This is illegal. Firstly,
non-type template parameters of class type are not allowed in C++. And
secondly, you don't even have a struct named 'A' in your program.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #4
"Agent Mulder" <mb************ *******@home.nl > wrote in message
news:bo******** **@news4.tilbu1 .nb.home.nl...
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

struct Jazz{};
struct Funk{};
struct Bach{};
template<struct A>struct Music{void play(){}};
struct Music<Bach>{}; //partial template specialization
int main()
{
Music<Jazz>().p lay(); //OK
Music<Funk>().p lay(); //OK
Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
return 0;
}

Maybe they use templates this way on the X-Files but... hehe

I would suggest this design instead:

class Music
{
public:
virtual void play() = 0;
virtual ~Music() {}
};

class Jazz : public Music
{
public:
virtual void play();
};

class Funk: public Music
{
public:
virtual void play();
};

etc.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #5
Thank you for your insightful responses. I fixed the design
by letting the template base and the template specialization
both inherit from the same class CD. This class CD has a virtual
play() method that gets called through a reference.

Still I am not satisfied. First, I need to forward declare struct
Bach. Second, both templates need to inherit class CD. What's the
use of template specialization if you lose everything that was
specified in the base template?

#include<iostre am>
struct Bach; //forward declaration
struct CD{virtual void play(){cout<<"\ nFeep-Honk-Feep-Honk";}};
template<class A>struct Music:CD{};
struct Music<Bach>:CD{ void play(){cout<<"\ nHonk-Honk-Feep-Feep";}};
struct Jazz{};
struct Funk{};
struct Bach{};
Music<Jazz>jazz ;
Music<Funk>funk ;
Music<Bach>bach ;
int main()
{
CD&jazz=::jazz ;
CD&funk=::funk ;
CD&bach=::bach ;
jazz.play();
funk.play();
bach.play();
return 0;
}

-------------------
output:
Feep-Honk-Feep-Honk
Feep-Honk-Feep-Honk
Honk-Honk-Feep-Feep

-X
Jul 19 '05 #6
"Agent Mulder" <mb************ *******@home.nl > wrote in message
news:bo******** **@news1.tilbu1 .nb.home.nl...
Thank you for your insightful responses. I fixed the design
by letting the template base and the template specialization
both inherit from the same class CD. This class CD has a virtual
play() method that gets called through a reference.

Still I am not satisfied. First, I need to forward declare struct
Bach. Second, both templates need to inherit class CD. What's the
use of template specialization if you lose everything that was
specified in the base template?


What indeed? You don't need any templates to do what you are trying to do.
They just confuse the issue.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #7
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message news:a0******** ***********@twi ster.nyroc.rr.c om...
"Agent Mulder" <mb************ *******@home.nl > wrote in message
news:bo******** **@news1.tilbu1 .nb.home.nl...
Thank you for your insightful responses. I fixed the design
by letting the template base and the template specialization
both inherit from the same class CD. This class CD has a virtual
play() method that gets called through a reference.

Still I am not satisfied. First, I need to forward declare struct
Bach. Second, both templates need to inherit class CD. What's the
use of template specialization if you lose everything that was
specified in the base template?


What indeed? You don't need any templates to do what you are trying to do.
They just confuse the issue.


Templates I try to understand, not the inner working of Music<Bach>::pl ay();
although you do seem to think so. Refer to Stroustrup 13.6 Derivation and
Templates for examples of how to organize your code around templates.

-X

I try to understand templates I try to understand. Not to implement Music<Jazz>::pl ay(),
although you do seem to think so.
Jul 19 '05 #8
In article <bo**********@n ews4.tilbu1.nb. home.nl>,
mb************* ******@home.nl says...
Hi group,

I have a problem with partial template specialization.
First and foremost that what you think is partial specialization is
really explicit specialization?

Partial specialization looks something like this:

template <class A, class B>
class X { ... };

template <class A>
class X<int, A> { ... };

The first is unspecialized -- either parameter can potentially be bound
to any type. The second is a partial specialization. We're specifying
what code will be produced when the first parameter happens to be int,
but the second parameter can still vary. The bottom line: to be partial
specialization, you have to have at least one each of specified
parameters and unspecified parameters. If you're specifying the types
of all parameters, you have an explicit specialization instead.
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?


No -- specialization is not inheritance. Each specialization (partial
or explicit) is independent of the primary template, and has to define
its own members. The members of a specialization may be completely
different from the members of the primary template.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6861
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
9
2545
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something like this: ----8<--- typedef char IsPODForListArrayTrue;
1
1736
by: BekTek | last post by:
I'm still confused about the template partial specialization which is used in many libraries.. due to lack of introduction for beginner.. Could you tell me about that in short? Thanks in advance..
5
6589
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
6
2765
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave no errors compiling this:
9
2784
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
9
1960
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... Exemple: template <class T, int Num> class A
10
2788
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid A<T,3>::f (T) {
1
2226
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9333
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,...
0
9254
tracyyun
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...
1
6799
isladogs
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...
0
6078
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();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.