Template specialization (not partial): what is it?

Discussion in 'C++' started by Markus, Dec 11, 2011.

  1. Markus

    Markus Guest

    Example: A template where the instantiation, depending on the
    parameterized type, needs a different implementation of one of the
    methods.

    template<typename T>
    class MyTemplateClass
    {
    T var;

    public:

    void DoSomething();
    };

    The specializations of the method are something like:

    template<>
    void MyTemplateClass<int>::DoSomething()
    { // do something specific to a template<int> }

    template<>
    void MyTemplateClass<double>::DoSomething()
    { // do something specific to a template<double> }


    Compare the specialized template approach to having DoSomething declared
    as a virtual function in the template. I see the specialized template
    approach as a way to avoid all the virtual function machinery. Is that
    pretty much the gist of it?

    I am noob to template specialization. It would be good to get some
    general perspective on the usefulness of specialization (both kinds), so
    if you have some rules of thumb as to how applicable and when applicable
    specialization is, do say.

    (I never got back to this, but a good topic, I think)
     
    Markus, Dec 11, 2011
    #1
    1. Advertising

  2. On 12/11/2011 3:29 AM, Markus wrote:
    > Example: A template where the instantiation, depending on the
    > parameterized type, needs a different implementation of one of the
    > methods.
    >
    > template<typename T>
    > class MyTemplateClass
    > {
    > T var;
    >
    > public:
    >
    > void DoSomething();
    > };
    >
    > The specializations of the method are something like:
    >
    > template<>
    > void MyTemplateClass<int>::DoSomething()
    > { // do something specific to a template<int> }
    >
    > template<>
    > void MyTemplateClass<double>::DoSomething()
    > { // do something specific to a template<double> }
    >
    >
    > Compare the specialized template approach to having DoSomething declared
    > as a virtual function in the template. I see the specialized template
    > approach as a way to avoid all the virtual function machinery. Is that
    > pretty much the gist of it?


    <shrug> Whatever motivation/rationale seems more sensible to you... My
    reaction to this is, "who cares?"

    I think that the full specialization of a single member is allowed to
    avoid having to specialize (and repeat) the entire class, which,
    compared to your particular case, can be very large and extensive, and
    differ in behavior of only a couple of member functions.

    > I am noob to template specialization. It would be good to get some
    > general perspective on the usefulness of specialization (both kinds), so
    > if you have some rules of thumb as to how applicable and when applicable
    > specialization is, do say.
    >
    > (I never got back to this, but a good topic, I think)


    Find a get a copy of "C++ Templates" by Vandevoorde and Josuttis.
    "Modern C++ Design" by Alexandrescu is also about templates, their use
    and tricks you can play. I am fairly certain that there's going to be a
    couple of new ones considering that templates (mainly in how they are
    used) have not stood still in the years since the first standard
    document that defined them.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 11, 2011
    #2
    1. Advertising

  3. On Sunday, December 11, 2011 10:19:25 PM UTC+8, Victor Bazarov wrote:
    > On 12/11/2011 3:29 AM, Markus wrote:
    > > Example: A template where the instantiation, depending on the
    > > parameterized type, needs a different implementation of one of the
    > > methods.
    > >
    > > template<typename T>
    > > class MyTemplateClass
    > > {
    > > T var;
    > >
    > > public:
    > >
    > > void DoSomething();
    > > };
    > >
    > > The specializations of the method are something like:
    > >
    > > template<>
    > > void MyTemplateClass<int>::DoSomething()
    > > { // do something specific to a template<int> }
    > >
    > > template<>
    > > void MyTemplateClass<double>::DoSomething()
    > > { // do something specific to a template<double> }
    > >
    > >
    > > Compare the specialized template approach to having DoSomething declared
    > > as a virtual function in the template. I see the specialized template
    > > approach as a way to avoid all the virtual function machinery. Is that
    > > pretty much the gist of it?

    >
    > <shrug> Whatever motivation/rationale seems more sensible to you... My
    > reaction to this is, "who cares?"
    >
    > I think that the full specialization of a single member is allowed to
    > avoid having to specialize (and repeat) the entire class, which,
    > compared to your particular case, can be very large and extensive, and
    > differ in behavior of only a couple of member functions.
    >
    > > I am noob to template specialization. It would be good to get some
    > > general perspective on the usefulness of specialization (both kinds), so
    > > if you have some rules of thumb as to how applicable and when applicable
    > > specialization is, do say.
    > >
    > > (I never got back to this, but a good topic, I think)

    >
    > Find a get a copy of "C++ Templates" by Vandevoorde and Josuttis.
    > "Modern C++ Design" by Alexandrescu is also about templates, their use
    > and tricks you can play. I am fairly certain that there's going to be a
    > couple of new ones considering that templates (mainly in how they are
    > used) have not stood still in the years since the first standard
    > document that defined them.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    A macro system without a compiler embedded is impossible.

    The template is good to for machines to generate programs to be run on machines
    toward the goal of smart robots that have to suck the carpete area in a room
    right first.
     
    88888 Dihedral, Dec 11, 2011
    #3
    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. Joseph Turian
    Replies:
    2
    Views:
    480
  2. toton
    Replies:
    1
    Views:
    624
  3. jrwats
    Replies:
    2
    Views:
    1,593
    jrwats
    Jul 11, 2009
  4. vj
    Replies:
    1
    Views:
    492
  5. Hizo
    Replies:
    17
    Views:
    703
    itaj sherman
    Mar 7, 2011
Loading...

Share This Page