473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stuck on partial specialization syntax, as usual.

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) {
}

The goal of that code is to partially specialize A<T,N>::f() for
certain values of N, leaving T unconstrained. The syntax is what I
could pick up from this site:

http://www.cprogramming.com/tutorial...alization.html

This code does not compile. Comeau (using whatever the default
settings are when you go to the test drive site) seems to produce the
clearest errors:

"ComeauTest.c", line 5: error: template argument list must match the
parameter list
template <typename Tvoid A<T,1>::f (T) {

"ComeauTest.c", line 8: error: template argument list must match the
parameter list
template <typename Tvoid A<T,3>::f (T) {

I can't seem to massage this into compiling. What is the right syntax
for this?

Thanks!
Jason
Jun 27 '08 #1
10 2748
On May 26, 1:12 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
This code does not compile. Comeau (using whatever the default
settings are when you go to the test drive site) seems to produce the
clearest errors:

"ComeauTest.c", line 5: error: template argument list must match the
parameter list
template <typename Tvoid A<T,1>::f (T) {

"ComeauTest.c", line 8: error: template argument list must match the
parameter list
template <typename Tvoid A<T,3>::f (T) {
Well, FWIW here is what GCC says, also (3.4.5 MinGW):

$ g++ pspec.cpp
pspec.cpp:5: error: invalid use of undefined type `class A<T, 1>'
pspec.cpp:1: error: declaration of `class A<T, 1>'
pspec.cpp:5: error: template definition of non-template `void A<T,
1>::f(T)'
pspec.cpp:8: error: invalid use of undefined type `class A<T, 3>'
pspec.cpp:1: error: declaration of `class A<T, 3>'
pspec.cpp:8: error: template definition of non-template `void A<T,
3>::f(T)'

Jason
Jun 27 '08 #2
ja************@gmail.com wrote:
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) {
}
You can't partially specialise an individual member of class template.

--
Ian Collins.
Jun 27 '08 #3
Thanks for your reply.

On May 26, 1:43 am, Ian Collins <ian-n...@hotmail.comwrote:
jason.cipri...@gmail.com wrote:
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) {
}

You can't partially specialise an individual member of class template.

:-(

So... if most compilers optimize away constant conditions; can I do
something like this instead:

template <typename T, int Nvoid A<T,N>::f (T) {
if (N == 1) {
} else if (N == 3) {
}
}

And (probably) not take a performance hit?

Thanks,
Jason
Jun 27 '08 #4
In article <d2ead088-61cc-4658-9c32-b4bedf22bd04@
59g2000hsb.googlegroups.com>, ja************@gmail.com says...
I never seem to be able to get this right. Here I have some code:

template <typename T, int Nclass A {
void f (T);
};
This is a class template.
template <typename Tvoid A<T,1>::f (T) {
}
This is an attempt at only the member function for a partially
specialized class template -- you need to start with the partial
specialization of the class template itself. Unlike inheritance (for one
example), partial specialization doesn't imply anything about the
partially specialized template having a structure similar to the
unspecialized template. They typically share (at least most of) the same
member functions, but you need to redeclare and re-define each member
function in every partial specialization. The language doesn't prevent
you from defining entirely different member functions in each partial
specialization, if you so choose.

template <class T, int N>
struct A {
void f(T) { std::cout << "Unspecialized.\n"; }
};

template <class T>
struct A<T, 1{
void f(T) { std::cout << "Spec 1\n"; }
};

template <class T>
struct A<T, 2{
void f(T) { std::cout << "Spec 2\n"; }
};

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #5
Thanks for your reply.
>
On May 26, 1:43 am, Ian Collins <ian-n...@hotmail.comwrote:
>jason.cipri...@gmail.com wrote:
>>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) {
}
You can't partially specialise an individual member of class template.


:-(

So... if most compilers optimize away constant conditions; can I do
something like this instead:

template <typename T, int Nvoid A<T,N>::f (T) {
if (N == 1) {
} else if (N == 3) {
}
}

And (probably) not take a performance hit?
template <typename T, int N>
class A {
void f(T);
};

template<typename T>
class A1: public A<T,1{};

template<typename T>
void A<T,1>::f(T) { /* ... */ }
Jun 27 '08 #6
On May 26, 1:51 am, Jerry Coffin <jcof...@taeus.comwrote:
This is an attempt at only the member function for a partially
specialized class template -- you need to start with the partial
specialization of the class template itself. Unlike inheritance (for one
example), partial specialization doesn't imply anything about the
partially specialized template having a structure similar to the
unspecialized template.
Thanks.

In my case, I have a fairly large and complex template class, but a
small handful of the member functions can be highly optimized for
certain template parameters. I'll have to rethink my design a little
and somehow split things up a little differently; it's very
inconvenient to have to maintain two copies of the code.

I'm almost considering hacking something together right now like:

--- template.h ---

template <class T>
struct A<T, SPECVAL{
void f(T) { std::cout << "Spec 1\n"; }
};

--- otherfile.h ---

#define SPECVAL 1
#include "template.h"
#undef SPECVAL
#define SPECVAL 2
#include "template.h"

---

I have a feeling that may make me some enemies some day. Too bad I
have a deadline in the morning :-(.

Thanks,
Jason
Jun 27 '08 #7
ja************@gmail.com wrote:
Thanks for your reply.

On May 26, 1:43 am, Ian Collins <ian-n...@hotmail.comwrote:
>jason.cipri...@gmail.com wrote:
>>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) {
}
You can't partially specialise an individual member of class template.
:-(

So... if most compilers optimize away constant conditions; can I do
something like this instead:

template <typename T, int Nvoid A<T,N>::f (T) {
if (N == 1) {
} else if (N == 3) {
}
}

And (probably) not take a performance hit?
Or try another level of indirection along the lines of:

#include <iostream>

template <typename T, int Nclass A
{
template <int NNstruct X{};

template <typename TTvoid fn( T t, TT )
{
std::cout << "called for " << N << std::endl;
}

void fn( T t, X<1)
{
std::cout << "specialised for 1" << std::endl;
}

public:

void f(T t)
{
fn( t, X<N>() );
}
};

int main()
{
A<int, 42>().f( 0 );
A<int, 1>().f( 0 );

return 0;
}

--
Ian Collins.
Jun 27 '08 #8
On May 26, 2:44 am, Ian Collins <ian-n...@hotmail.comwrote:
Or try another level of indirection along the lines of:
That's pretty clever; thanks!

Jason
Jun 27 '08 #9
On May 26, 7:48 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
Thanks for your reply.
On May 26, 1:43 am, Ian Collins <ian-n...@hotmail.comwrote:
jason.cipri...@gmail.com wrote:
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) {
}
You can't partially specialise an individual member of class template.
:-(
So... if most compilers optimize away constant conditions; can I do
something like this instead:
template <typename T, int Nvoid A<T,N>::f (T) {
if (N == 1) {
} else if (N == 3) {
}
}
And (probably) not take a performance hit?
Probably. The usual solution is to use a helper function and a
discriminator class, something like:

template< typename T, int N class A {
{
template< int N >
class Discriminator;

void f( T t ) {
{
f( t, Discriminator< N >() ) ;
}

void f( T t, Discriminator<1) {
// specialization for 1...
}
void f( T t, Discriminator<3) {
// specialization for 3...
}
} ;

Presumably, because it is the standard idiom, compilers will
recognize it, and not generate any extra code for the unused
argument (at least as long as the functions are inline).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10
In article <21085279-b67d-44f1-a883-fce64b362eb5@
2g2000hsn.googlegroups.com>, ja************@gmail.com says...

[ ... ]
In my case, I have a fairly large and complex template class, but a
small handful of the member functions can be highly optimized for
certain template parameters. I'll have to rethink my design a little
and somehow split things up a little differently; it's very
inconvenient to have to maintain two copies of the code.
It sounds like Ian Collins's suggestion fits your situation almost
perfectly.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #11

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

Similar topics

17
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...
9
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...
8
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...
1
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...
6
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...
9
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...
2
by: VB | last post by:
Hi, I was attempting to implement partial specialization using VC 2005. However i cannot get my code to compile :( .. Can you please help me? template <typename TYPE1> class myClass<TYPE1,...
1
by: toton | last post by:
Hi, I am doing some template specialization for a template class, where specialization is done on a member function. I am not able to get exact syntax for it. The code is give as below, enum...
9
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... ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
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,...
0
jinu1996
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...

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.