Template specialization (not partial): what is it?

M

Markus

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)
 
V

Victor Bazarov

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
 
8

88888 Dihedral

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


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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top