Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > help on generic functions

Reply
Thread Tools

help on generic functions

 
 
shuisheng
Guest
Posts: n/a
 
      09-20-2006
Dear All,

Would you please help me to look at this simple case:

Assume I have a function to calculate the power of base ^ order. Here
base can be of any proper type, and order of type size_t.

//! Power with order > 0.
/*!
\param[in] base base.
*/
template<class Type, size_t order>
inline Type Power(Type base)
{
return base * Power<Type, order-1>(base);
}

//! Power with order = 0.
/*!
\param[in] base base.
*/
template<class Type, 0>
inline Type Power<Type, 0>(Type base)
{
return 1;
}

I got some compiler error for the second function Power<Type, 0> to
particularize the Power function.

Thanks for your help!

Shuisheng

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      09-20-2006

shuisheng wrote:


> Assume I have a function to calculate the power of base ^ order. Here
> base can be of any proper type, and order of type size_t.
>
> //! Power with order > 0.
> /*!
> \param[in] base base.
> */
> template<class Type, size_t order>
> inline Type Power(Type base)
> {
> return base * Power<Type, order-1>(base);
> }
>
> //! Power with order = 0.
> /*!
> \param[in] base base.
> */
> template<class Type, 0>
> inline Type Power<Type, 0>(Type base)
> {
> return 1;
> }
>
> I got some compiler error for the second function Power<Type, 0> to
> particularize the Power function.


You can't partially specialize function templates. You must either
make it a full specialization, or else switch to class templates, which
can be partially specialized.

Best regards,

Tom

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      09-20-2006
On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
<(E-Mail Removed)> wrote,
>I got some compiler error for the second function Power<Type, 0> to
>particularize the Power function.


By the way, it's called "specialize" instead of "particularize".

Specialization of function templates is not allowed, only of class
templates. In most cases, ordinary function overloading serves
instead of specialization. Here is an example with class templates:


#include <iostream>
template<class Type, size_t order>
class p {
public: static inline Type Power(Type base)
{
return base * p<Type, order-1>:ower(base);
}
};

template<class Type>
class p<Type,0> {
public: static inline Type Power(Type base)
{
return 1;
}
};

int main()
{
int b = 5;
std::cout << p<int,3>:ower(b);
}

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      09-20-2006
David Harmon wrote:

> On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
> <(E-Mail Removed)> wrote,
> >I got some compiler error for the second function Power<Type, 0> to
> >particularize the Power function.

>
> By the way, it's called "specialize" instead of "particularize".
>
> Specialization of function templates is not allowed, only of class
> templates.


I think you mean _partial_ specialization of function templates is not
allowed. Full (or "explicit") specialization of function templates is
allowed. The OP's problem was that s/he has two template parameters,
but only wanted to specialize one of them. The OP could have stuck
with function templates by using only one template parameter, for
example, and fully specializing it.

Best regards,

Tom

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      09-20-2006
On 20 Sep 2006 13:18:25 -0700 in comp.lang.c++, "Thomas Tutone"
<(E-Mail Removed)> wrote,
>David Harmon wrote:
>
>> On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
>> <(E-Mail Removed)> wrote,

>I think you mean _partial_ specialization of function templates is not
>allowed. Full (or "explicit") specialization of function templates is
>allowed.


Yes, _partial_ specialization is what I meant. Thanks for the
correction.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
generic functions(template functions) Tony Johansson C++ 3 08-16-2005 10:33 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments