Re: Stuck on partial specialization syntax, as usual.
On May 26, 7:48 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
Thanks for your reply.
On May 26, 1:43 am, Ian Collins <ian-n...@hotmail.com> wrote:
jason.cipri...@gmail.com wrote:
I never seem to be able to get this right. Here I have some code:
template <typename T, int N> class A {
void f (T);
};
template <typename T> void 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 N> void 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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34