template specification

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

    template specification

    Just to make it sure:

    it is not possibel in C++ to have a template specialization
    for more than one type, right?

    ie. I have several template type parameters like
    string, int, long, char, float, double, complex.

    The template for float and double is exactly the same,
    the one for the integer types as well.
    So I can make the default case for the integer types, but
    I have to repeat the code for the floating type specialization, right?

    thanks,
    marc

  • tom_usenet

    #2
    Re: template specification

    On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens
    <m_schellens@ho tmail.com> wrote:
    [color=blue]
    >Just to make it sure:
    >
    >it is not possibel in C++ to have a template specialization
    >for more than one type, right?[/color]

    Right.
    [color=blue]
    >
    >ie. I have several template type parameters like
    >string, int, long, char, float, double, complex.
    >
    >The template for float and double is exactly the same,
    >the one for the integer types as well.
    >So I can make the default case for the integer types, but
    >I have to repeat the code for the floating type specialization, right?[/color]

    Well, there are lots of ways you could share the code rather than
    repeat it. Inheritence is one way. e.g.

    template <class T>
    class MyClassFloat
    {
    //...
    };

    template<>
    class MyClass<float>: public FloatImpl<float > {};
    template<>
    class MyClass<double> : public FloatImpl<doubl e> {};

    Another way is to use indirection to the implementation through
    traits, and bind float and double to the same traits.

    Tom

    Comment

    • Marc Schellens

      #3
      Re: template specification

      > On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens[color=blue]
      > <m_schellens@ho tmail.com> wrote:
      >
      >[color=green]
      >>Just to make it sure:
      >>
      >>it is not possible in C++ to have a template specialization
      >>for more than one type, right?[/color]
      >
      >
      > Right.
      >
      >[color=green]
      >>ie. I have several template type parameters like
      >>string, int, long, char, float, double, complex.
      >>
      >>The template for float and double is exactly the same,
      >>the one for the integer types as well.
      >>So I can make the default case for the integer types, but
      >>I have to repeat the code for the floating type specialization, right?[/color]
      >
      >
      > Well, there are lots of ways you could share the code rather than
      > repeat it. Inheritence is one way. e.g.
      >
      > template <class T>
      > class MyClassFloat
      > {
      > //...
      > };
      >
      > template<>
      > class MyClass<float>: public FloatImpl<float > {};
      > template<>
      > class MyClass<double> : public FloatImpl<doubl e> {};
      >
      > Another way is to use indirection to the implementation through
      > traits, and bind float and double to the same traits.
      >
      > Tom[/color]

      Thanks Tom,

      but I was not specific enough, sorry:
      I meant a member function specialization.

      Any suggestions for that?
      thanks,
      marc

      Comment

      • tom_usenet

        #4
        Re: template specification

        On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
        <m_schellens@ho tmail.com> wrote:
        [color=blue]
        >but I was not specific enough, sorry:
        >I meant a member function specialization.
        >
        >Any suggestions for that?[/color]

        template<class T, bool floatimpl, bool otherimpl>
        struct memfunimplchoos er
        {
        static void run(T t)
        {
        //default impl
        }
        }

        template <class T>
        struct memfunimplchoos er<T, true, false>
        {
        static void run(T t)
        {
        //fp impl
        }
        };

        //etc. other impls.


        //and the actual function
        template <class T>
        void memfun(T t)
        {
        memfunimplchoos er<T, is_float<T>::va lue,
        is_something_el se<T>::value>:: run(t);
        }

        Tom

        Comment

        • Marc Schellens

          #5
          Re: template specification

          tom_usenet wrote:[color=blue]
          > On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
          > <m_schellens@ho tmail.com> wrote:
          >
          >[color=green]
          >>but I was not specific enough, sorry:
          >>I meant a member function specialization.
          >>
          >>Any suggestions for that?[/color]
          >
          >
          > template<class T, bool floatimpl, bool otherimpl>
          > struct memfunimplchoos er
          > {
          > static void run(T t)
          > {
          > //default impl
          > }
          > }
          >
          > template <class T>
          > struct memfunimplchoos er<T, true, false>
          > {
          > static void run(T t)
          > {
          > //fp impl
          > }
          > };
          >
          > //etc. other impls.
          >
          >
          > //and the actual function
          > template <class T>
          > void memfun(T t)
          > {
          > memfunimplchoos er<T, is_float<T>::va lue,
          > is_something_el se<T>::value>:: run(t);
          > }
          >
          > Tom[/color]

          A bit ugly I think.
          But thank you very much for the suggestion,
          marc


          Comment

          Working...