help on generic functions

Discussion in 'C++' started by shuisheng, Sep 20, 2006.

  1. shuisheng

    shuisheng Guest

    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
    shuisheng, Sep 20, 2006
    #1
    1. Advertising

  2. 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
    Thomas Tutone, Sep 20, 2006
    #2
    1. Advertising

  3. shuisheng

    David Harmon Guest

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

    > On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
    > <> 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
    Thomas Tutone, Sep 20, 2006
    #4
  5. shuisheng

    David Harmon Guest

    On 20 Sep 2006 13:18:25 -0700 in comp.lang.c++, "Thomas Tutone"
    <> wrote,
    >David Harmon wrote:
    >
    >> On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
    >> <> 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.
    David Harmon, Sep 20, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,027
    Chaitanya
    Feb 3, 2009
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,577
    Victor Bazarov
    Jun 21, 2004
  3. Replies:
    2
    Views:
    424
  4. Tony Johansson

    generic functions(template functions)

    Tony Johansson, Aug 16, 2005, in forum: C++
    Replies:
    3
    Views:
    398
    Srini
    Aug 16, 2005
  5. minlearn
    Replies:
    2
    Views:
    445
    red floyd
    Mar 13, 2009
Loading...

Share This Page