help on generic functions

S

shuisheng

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
 
T

Thomas Tutone

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
 
D

David Harmon

On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
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>::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);
}
 
T

Thomas Tutone

David said:
On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"


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
 
D

David Harmon

On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
<[email protected]> wrote,
I think you mean _partial_ specialization of function templates is not
allowed. Full (or "explicit") specialization of function templates is
allowed.[/QUOTE]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top