473,239 Members | 1,552 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,239 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 2731
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.