template specification

Discussion in 'C++' started by Marc Schellens, Oct 2, 2003.

  1. 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
    Marc Schellens, Oct 2, 2003
    #1
    1. Advertising

  2. Marc Schellens

    tom_usenet Guest

    On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens
    <> wrote:

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


    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?


    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<double> {};

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

    Tom
    tom_usenet, Oct 2, 2003
    #2
    1. Advertising

  3. > On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens
    > <> wrote:
    >
    >
    >>Just to make it sure:
    >>
    >>it is not possible in C++ to have a template specialization
    >>for more than one type, right?

    >
    >
    > 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?

    >
    >
    > 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<double> {};
    >
    > Another way is to use indirection to the implementation through
    > traits, and bind float and double to the same traits.
    >
    > Tom


    Thanks Tom,

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

    Any suggestions for that?
    thanks,
    marc
    Marc Schellens, Oct 3, 2003
    #3
  4. Marc Schellens

    tom_usenet Guest

    On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
    <> wrote:

    >but I was not specific enough, sorry:
    >I meant a member function specialization.
    >
    >Any suggestions for that?


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

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

    //etc. other impls.


    //and the actual function
    template <class T>
    void memfun(T t)
    {
    memfunimplchooser<T, is_float<T>::value,
    is_something_else<T>::value>::run(t);
    }

    Tom
    tom_usenet, Oct 3, 2003
    #4
  5. tom_usenet wrote:
    > On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
    > <> wrote:
    >
    >
    >>but I was not specific enough, sorry:
    >>I meant a member function specialization.
    >>
    >>Any suggestions for that?

    >
    >
    > template<class T, bool floatimpl, bool otherimpl>
    > struct memfunimplchooser
    > {
    > static void run(T t)
    > {
    > //default impl
    > }
    > }
    >
    > template <class T>
    > struct memfunimplchooser<T, true, false>
    > {
    > static void run(T t)
    > {
    > //fp impl
    > }
    > };
    >
    > //etc. other impls.
    >
    >
    > //and the actual function
    > template <class T>
    > void memfun(T t)
    > {
    > memfunimplchooser<T, is_float<T>::value,
    > is_something_else<T>::value>::run(t);
    > }
    >
    > Tom


    A bit ugly I think.
    But thank you very much for the suggestion,
    marc
    Marc Schellens, Oct 4, 2003
    #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. Chris Theis
    Replies:
    2
    Views:
    453
    Chris Theis
    Jul 24, 2003
  2. Marek Vondrak
    Replies:
    9
    Views:
    321
    Marek Vondrak
    May 15, 2006
  3. Victor Bazarov

    Re: template specification oddness

    Victor Bazarov, Feb 21, 2008, in forum: C++
    Replies:
    3
    Views:
    303
    Matthias Buelow
    Feb 21, 2008
  4. Matthias Buelow

    template specification oddness

    Matthias Buelow, Feb 21, 2008, in forum: C++
    Replies:
    4
    Views:
    332
    James Kanze
    Feb 23, 2008
  5. Replies:
    10
    Views:
    495
    Eric Pruneau
    Jun 20, 2008
Loading...

Share This Page