help on generic functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shuisheng

    #1

    help on generic functions

    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

  • Thomas Tutone

    #2
    Re: help on generic functions


    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

    Comment

    • David Harmon

      #3
      Re: help on generic functions

      On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
      <shuisheng75@ya hoo.comwrote,
      >I got some compiler error for the second function Power<Type, 0to
      >particulariz e 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);
      }

      Comment

      • Thomas Tutone

        #4
        Re: help on generic functions

        David Harmon wrote:
        On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
        <shuisheng75@ya hoo.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

        Comment

        • David Harmon

          #5
          Re: help on generic functions

          On 20 Sep 2006 13:18:25 -0700 in comp.lang.c++, "Thomas Tutone"
          <Thomas8675309@ yahoo.comwrote,
          >David Harmon wrote:
          >
          >On 20 Sep 2006 08:35:28 -0700 in comp.lang.c++, "shuisheng"
          ><shuisheng75@y ahoo.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.

          Comment

          Working...