about partial specialization

Discussion in 'C++' started by Rex_chaos, Oct 23, 2003.

  1. Rex_chaos

    Rex_chaos Guest

    Hi there,
    I am writing a template class like

    template <class T, int rank>
    class TC {...};

    In my case, I would like to partial specialize the class.

    template <class T>
    class TC<T,1> {...};

    template <class T>
    class TC<T,2> {...};

    template <class T>
    class TC<T,2> {...};

    Within each class, there is a little change. A particular function
    will be added to the class and the implementation of some function
    will also be modified. I wonder it it's necessary to "copy and paste"
    all the code from the original template class to the specialized one.
    It's very annoying if I have to.

    For this purpose, I wonder if it's better to use the inheritance
    instead.

    template <class T>
    class TCOne :public TC<T,1>
    {...};

    For my case, the efficiency is the most essential factor. Will it
    lower the efficiency if I take the inheitance mechanism?

    Thanks in advance.
     
    Rex_chaos, Oct 23, 2003
    #1
    1. Advertising

  2. Rex_chaos wrote:
    ....
    > For this purpose, I wonder if it's better to use the inheritance
    > instead.
    >
    > template <class T>
    > class TCOne :public TC<T,1>
    > {...};
    >
    > For my case, the efficiency is the most essential factor. Will it
    > lower the efficiency if I take the inheitance mechanism?


    No. Inheritance has very low (if no) run time cost.


    By the description, I thought you meant to do this.

    template <class T>
    class TC<T,1> : public BASE
    {...};
     
    Gianni Mariani, Oct 23, 2003
    #2
    1. Advertising

  3. On 23 Oct 2003 08:29:54 -0700, Rex_chaos <> wrote:

    > Hi there,
    > I am writing a template class like
    >
    > template <class T, int rank>
    > class TC {...};
    >
    > In my case, I would like to partial specialize the class.
    >
    > template <class T>
    > class TC<T,1> {...};
    >
    > template <class T>
    > class TC<T,2> {...};
    >
    > template <class T>
    > class TC<T,2> {...};
    >
    > Within each class, there is a little change. A particular function
    > will be added to the class and the implementation of some function
    > will also be modified. I wonder it it's necessary to "copy and paste"
    > all the code from the original template class to the specialized one.
    > It's very annoying if I have to.


    And it's a maintanance mess.

    >
    > For this purpose, I wonder if it's better to use the inheritance
    > instead.


    Separate repeated code and put it into the base class.

    >
    > template <class T>
    > class TCOne :public TC<T,1> {...};
    >
    > For my case, the efficiency is the most essential factor. Will it
    > lower the efficiency if I take the inheitance mechanism?

    I don't think so.

    >
    > Thanks in advance.
    >


    You also could use macro to avoid repeating code.
    It's ugly but if there is nothing better why not use it.

    --
    grzegorz
     
    Grzegorz Sakrejda, Oct 23, 2003
    #3
  4. Rex_chaos

    stephan beal Guest

    Gianni Mariani wrote:
    >> template <class T>
    >> class TCOne :public TC<T,1>
    >> {...};

    ....
    > By the description, I thought you meant to do this.
    >
    > template <class T>
    > class TC<T,1> : public BASE
    > {...};


    No, he means for a specialization to subclass the class template which it is
    specializing. Convoluted, perhaps, but i've done this in the past as well.
    e.g.,

    template <class SerializableType,class NodeType = s11n::s11n_node>
    class serializable_adapter
    {
    ....
    };

    /**
    Quasi-bogus specialization to accomodate usage of, e.g.,
    container::value_type as a SerializableType parameter.
    This feature is used by, e.g., de/serialize_{map,list}() to
    strip the pointer part from value_type so it can be re-used
    as a template parameter for other functions.
    */
    template <class SerializableType,class NodeType>
    class serializable_adapter<SerializableType *,NodeType> : public
    serializable_adapter <SerializableType,NodeType>{};

    So, in end effect this causes pointer and non-pointer SerializableTypes to
    be treated identically (at least in the cases i've had so far).

    --
    ----- stephan beal
    Registered Linux User #71917 http://counter.li.org
    I speak for myself, not my employer. Contents may
    be hot. Slippery when wet. Reading disclaimers makes
    you go blind. Writing them is worse. You have been Warned.
     
    stephan beal, Oct 24, 2003
    #4
    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. Chandra Shekhar Kumar

    template partial specialization

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    17
    Views:
    3,035
    tom_usenet
    Jun 25, 2003
  2. Philip Lawatsch

    Partial Specialization workaround

    Philip Lawatsch, Jul 16, 2003, in forum: C++
    Replies:
    9
    Views:
    398
    Philip Lawatsch
    Jul 16, 2003
  3. Alexander Stippler

    partial specialization of function template

    Alexander Stippler, Aug 15, 2003, in forum: C++
    Replies:
    1
    Views:
    555
    Rob Williscroft
    Aug 15, 2003
  4. Petre Iantu
    Replies:
    1
    Views:
    354
    Petre Iantu
    Aug 17, 2003
  5. Joseph Turian
    Replies:
    2
    Views:
    471
Loading...

Share This Page