Using a simple example (summation), you will learn the basic techniques of generic programming. Starting from a simple C-style function, we gradually "lift" it to a fully generic component

A short tutorial on generic programming in C++

I still believe in abstraction, but now I know that one ends with abstraction, not starts with it. I learned that one has to adapt abstractions to reality and not the other way around.
Alexander Stepanov

We have already touched the question on the why of generic programming. Now, you may read a lot about generic programming, but to really understand it, you must do it. Thus, we'll now get our hands dirty with a concrete example of generic programming (or "put butter to the fish", as one says in a certain region of Germany). Step by step, we'll learn how to "lift" a simple, traditional summation procedure to a fully generic one, as shown in the following little menu:

Let' go! © Africa Studio -
  1. Summation a la C, please. (Doing it the traditional way.)
  2. Is there only a double version? (Parametrizing the value type)
  3. Is there only an array version? (Parametrizing iteration)
  4. Gimme back the value type! (Getting back the value type from the iterator)
  5. What's for starters? (A sum must be initialized — but not always with 0.)
  6. Which value type, please? (The value type of a sequence might not be the best one for summation.)
  7. Could you recommend something to start with? (Finding the right initialization automatically.)
  8. Can you add only? (Parametrizing the operator: We can as well multiply, find the max, ...)
  9. Back to the start (How to initialize a maximum?)
  10. Dessert: What else can we do with reduce? (Quite a bit. But some things perhaps better not)
  11. Food critique: What's the big deal? (Yes, in spite of some nuisances)