By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,192 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,192 IT Pros & Developers. It's quick & easy.

help on generic functions

P: n/a
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, 0to
particularize the Power function.

Thanks for your help!

Shuisheng

Sep 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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, 0to
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

Sep 20 '06 #2

P: n/a
On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
<sh*********@yahoo.comwrote,
>I got some compiler error for the second function Power<Type, 0to
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>::Power(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>::Power(b);
}

Sep 20 '06 #3

P: n/a
David Harmon wrote:
On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
<sh*********@yahoo.comwrote,
I got some compiler error for the second function Power<Type, 0to
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

Sep 20 '06 #4

P: n/a
On 20 Sep 2006 13:18:25 -0700 in comp.lang.c++, "Thomas Tutone"
<Th***********@yahoo.comwrote,
>David Harmon wrote:
>On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
<sh*********@yahoo.comwrote,
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.

Sep 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.