views:

309

answers:

8

One could break the question into two: how to read and to write templated code.

It is very easy to say, "it you want an array of doubles, write std::vector<double>", but it won't teach them how the templates work.

+3  A: 

There is a saying: "If you can't explain it, you don't understand it."

You can break it down further: How to write code that uses templated code, and how to write code that provides a templated service to others.

The basic explanation is that templates generated code based on a template. That is the source of the term "meta programming". It is programming how programming should be done.

The essential complexity of a vector is not that it is a vector of doubles (or type T), but that it is a vector. The basic structure is the same and templates separate that which is consistent from that which is not.

Further explanation depends on how much of that makes sense to you!

janm
You quoted that saying and then didnt really explain it your self...
Chalkey
OK, which bit didn't make sense to you?
janm
It doesn't make sense to me to say stroustrup doesn't understand templates, just because he can't explain them to a one year old. If that's not what you're implying (i.e that the questioner doesn't understand them), i'm not sure what you want to say by quoting it.
Johannes Schaub - litb
That doesn't make sense to me either. It misses the point: he could explain templates and the prerequisites but the 1 year old needs to understand. For explanation to work the listener needs to understand each prerequisite before going to the next step. So: Stroustrup could explain it to a one year old, but the one year old wouldn't be one year old by the time he'd finished. That's the problem with explaining; you need to figure out what the listener didn't understand.
janm
Johannes: Rereading your comment, I think I missed something. There are two issues. You need (1) a listener capable of understanding and (2) to be able to explain what the listener is missing. Your example is relevant: A 1 y/o child fails to meet requirement (1). The point of my quote is that if you have a point (2) failure, the problem could be your own lack of understanding. The follow-on is that the process of explaining can help you understand. I think that is one of the things that makes SO successful.
janm
IMHO your answer is not the paradigm of clarity either. Perhaps you'd want to make a distinction between 'metaprogramming' (which connotes Turing-completeness) and plain 'generic programming'. Your paragraph about the 'complexity of a vector' merely covers generic programming.
int3
A: 

You can tell that a template is a half-written source with parameters to be filled while instatiating the template.

sharptooth
+1  A: 

IMHO it is best to explain them as (very) fancy macros. They just work at higher level than C-style text substitution macros.

hrnt
+1  A: 

I found it very instructive to look at duck-typed languages. It doesn't matter, there, what type of argument you give a function, as long as they offer the right interface.

Templates allows to do the same thing: you can take any type, as long as the right interface is present. The additional benefit over duck-typing is, that the interface is checked at compile-time.

xtofl
Why the downvote? I find the analogy with duck typing appropriate, as I experienced it the other way myself: As a (mainly) C++ speaker learning Ruby, I really understood what they meant by "duck typing" the day I made that link with C++ function templates.
Éric Malenfant
I believe somebody downvoted most of the answers here. I got an unexplained -1 at the same time as almost everybody else.
hrnt
+1  A: 

Present them as advanced macros. It's a programming language on its own that is executed during compliation.

Tristram Gräbener
+1  A: 

I would get them to implement something themselves, then experiment with different variations until they understand it. Learning by doing is almost always the better option with programming.

For example, get them to make a template which compares two values and returns the higher one. Then have them see has passing ints or doubles or whatever still allows it to work. Then get them to tweak the the code / copy it and have it return the minimum value. Again, experiment with variations - will the template allow them to pass an int and a double, or will it complain?

From there, you can have them pass in arrays of whatever type (int, double etc), and have it sort the array from highest to lowest, again encouraging experimentation. From there, start to move into templated class definitions, using the same kind of ideas but on a larger scale. This is pretty much how I learnt about templates, ending up with complex array manipulation classes for generic types.

Smallgods
+6  A: 

I'd probably try to demonstrate the power of templates, by demonstrating the annoyance of not using them.

A good demonstration would be to write something simple like a stack of doubles (hand-written, not STL), with methods push, pop, and foldTopTwo, which pops off and adds together the top two values in the stack, and pushes the new value back on.

Then tell them to do the same for ints (or whatever, just some different numeric type).

Then show them how, by writing this stack as a template, you can significantly reduce the number of lines of code, and all of that horrible duplication.

Paul Butcher
Back when I was starting out I wrote an entire list class based on void* before I understood templates. That was one big slap to the forehead.
Matt Ellen
A: 

When I was teaching myself C++ I used this site a lot. It explains templates in depth and very well. I would recommend having them read that and try implementing something simple.

For a shorter explanation: Templates are frameworks for complicated constructs that act on data without having to know what that data is. Give them some examples of a simple template (like a linked-list) and walk through how the template is used to generate the final class.

Oz