Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

function template partial specialization

10 views
Skip to first unread message

Ralf Stoffels

unread,
Feb 26, 1999, 3:00:00 AM2/26/99
to
Why do I get the error
"template-id `f<T, 2>' in declaration of primary template" (egcs 1.1.1)
while compiling this:

template<typename T, int N> void f(T t) {}
template<typename T> void f<T,2>(T t) {}


It works fine without the first parameter:
template<int N> void f() {}
template<> void f<2>() {}


Ralf

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Biju Thomas

unread,
Feb 28, 1999, 3:00:00 AM2/28/99
to
Ralf Stoffels wrote:
>
> Why do I get the error
> "template-id `f<T, 2>' in declaration of primary template" (egcs 1.1.1)
> while compiling this:
>
> template<typename T, int N> void f(T t) {}
> template<typename T> void f<T,2>(T t) {}
>

There is nothing equivalent to class template partial specialization for
the case of function templates. Instead, function templates can be
overloaded. The following is legal:

template < typename T > void f ( T t ) {}

> It works fine without the first parameter:
> template<int N> void f() {}
> template<> void f<2>() {}

This is explicit specialization of the function template and is legal.

--
Best regards,
Biju Thomas

Alex Vinokur

unread,
Mar 3, 1999, 3:00:00 AM3/3/99
to
In article <36D69039...@faho.rwth-aachen.de>,

Ralf Stoffels <stof...@faho.rwth-aachen.de> wrote:
> Why do I get the error
> "template-id `f<T, 2>' in declaration of primary template" (egcs
1.1.1)
> while compiling this:
>
> template<typename T, int N> void f(T t) {}
> template<typename T> void f<T,2>(T t) {}
>
> It works fine without the first parameter:
> template<int N> void f() {}
> template<> void f<2>() {}
>
> Ralf

>
> [ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
> [ about comp.lang.c++.moderated. First time posters: do this! ]
>
Hi,

Yes it does not work with template-function
(why? - can anybody explain the compiler behavior?).

But we can do it using template class which has static function.


Thanks in advance,
Alex

########################################################
######## 1. main.C #####################################
########################################################

#include <string>

template <typename T, int N>

class FOO
{
public :
static void foo (T t)
{
cout << __PRETTY_FUNCTION__
<< "; t = "
<< t
<< "; N = "
<< N
<< endl;
// __PRETTY_FUNCTION__ is
// predefined variable
// in GNU gcc/g++
}
};

template <typename T>
class FOO <T, 2>
{
public :
static void foo (T t)
{
cout << __PRETTY_FUNCTION__
<< "; t = "
<< t
<< " (Special Case)"
<< endl;
}
};

int main ()
{
FOO<int, 1>::foo (222);
FOO<int, 2>::foo (333);
FOO<int, 3>::foo (444);
FOO<char, 1>::foo ('a');
FOO<char, 2>::foo ('b');
FOO<char, 3>::foo ('c');

return 0;
}

########################################################
######## 2. The results of the running #################
########################################################

static void FOO<int,1>::foo<int, 1>(int); t = 222; N = 1
static void FOO<int,2>::foo<int>(int); t = 333 (Special Case)
static void FOO<int,3>::foo<int, 3>(int); t = 444; N = 3
static void FOO<char,1>::foo<char, 1>(char); t = a; N = 1
static void FOO<char,2>::foo<char>(char); t = b (Special Case)
static void FOO<char,3>::foo<char, 3>(char); t = c; N = 3

########################################################
######## 3. Compiler & System ##########################
########################################################

g++ -v : gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
uname -a : SunOS tibamsun8 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5


########################################################
######## END of EXAMPLE ################################
########################################################

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Ralf Stoffels

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
> > template<typename T, int N> void f(T t) {}
> > template<typename T> void f<T,2>(T t) {}

> Yes it does not work with template-function


> (why? - can anybody explain the compiler behavior?).

I assume the reason is the overloading of function templates.
It would be impossible for the compiler to distinguish between
template<typename T> void f<T,2>(T t);
template<typename T> void f(T t);
unless you use explicit template argument specialization.


> But we can do it using template class which has static function.

Yes, I did it with this approach.
BTW: I used it for a template function witch perform a fast calculation
of "pow" for integer exponents.

What do you think about this implementation ?
Does it make sense to use this template meta programming or is it
more a code bloat ?
Is it the fastest approach even for very small and very large exponents
?

namespace _Pow {

template<bool odd, typename T> struct isOdd;

template <typename T>
struct isOdd<false,T>
{static inline void go(T& y, T q) {}
};

template <typename T>
struct isOdd<true,T>
{static inline void go(T& y, T q) {y*=q;}
};


template<unsigned int n, typename T>
struct Pow
{static inline T go(T y, T q)
{isOdd<n%2 == 1,T>::go(y,q);
return Pow<n/2,T>::go(y,q*q);
}
};

template<typename T>
struct Pow<1,T>
{static inline T go(T y, T q) {return y*q;}
};

template<typename T>
struct Pow<0,T>
{static inline T go(T y, T q) {return 1;} // 0/0 ignored
};


template<bool neg, int n, typename T> struct isNeg;

template <int n, typename T>
struct isNeg<false,n,T>
{static inline T go(T x) {return Pow<n,T>::go(1,x);}
};

template <int n, typename T>
struct isNeg<true,n,T>
{static inline T go(T x) {return 1/Pow<-n,T>::go(1,x);}
};

} // _Pow


template<int n, typename T>
inline T Pow(T x)
{return _Pow::isNeg<(n<0),n,T>::go(x);}

Alex Vinokur

unread,
Mar 16, 1999, 3:00:00 AM3/16/99
to
In article <36DD96F7...@faho.rwth-aachen.de>,
Ralf Stoffels <stof...@faho.rwth-aachen.de> wrote:
[snip]

>
> > But we can do it using template class which has static function.
>
> Yes, I did it with this approach.
> BTW: I used it for a template function witch perform a fast
calculation
> of "pow" for integer exponents.
>
> What do you think about this implementation ?
The implementation looks very mathematical (and it is its advantage).

> Does it make sense to use this template meta programming or is it
> more a code bloat ?

Yes, it does.
The approach is convenient and natural, so it makes sense.
(Number of the code lines is important thing,
but it is not the most important in algorithm/program).
I think the programmer/researcher/analyst
is more important *thing* than computer,
so convenience (in various senses)
is more important tnan number of the code lines.
This solution *template language* is suited for the problem,
so (I think) it is good solution.

> Is it the fastest approach even for very small and very large
exponents
> ?
>

[snip]

The computational time-complexity of the approach
needs to be evaluated and measured.

If you work on Solaris the gethrtime() and gethrvtime() functions may be
used
(see man gethrtime, gethrvtime - get high resolution time).

About time-comlexity measuring see also:

1.The *STL and Usual C : Average time-cost of execution* thread. The
link
is :
http://x11.dejanews.com/dnquery.xp?search=thread&svcclass=dnserver&threa
ded=1
&ST=PS&CONTEXT=914841639.1172307970&HIT_CONTEXT=914841639.1172307970&HIT
_NUM=
19&recnum=%3c36822F3...@tibam.elex.co.il%3e%231/3

2.The *Corrected and Supplemented -> Re: STL and Usual C : Average
time-cost of execution* thread. The links are :

http://x14.dejanews.com/dnquery.xp?search=thread&svcclass=dnserver&threa
ded=1&ST=PS&CONTEXT=914915711.1193541737&HIT_CONTEXT=914915711.119354173
7&HIT_NUM=0&recnum=%3c368734F...@tibam.elex.co.il%3e%231/3

http://x8.dejanews.com/dnquery.xp?&search=thread&svcclass=dnserver&ST=PS
&CONTEXT=919159530.506724404&HIT_CONTEXT=919159530.506724404&HIT_NUM=8&r
ecnum=%3c36872AC...@tibam.elex.co.il%3e%233/3

Alex

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

0 new messages