Boost logo

Boost :

From: Simon Buchan (simon_at_[hidden])
Date: 2005-09-26 01:11:17


Geoffrey Romer wrote:
> I think you're assuming that when I said "polymorphism", I meant
> "inheritance and virtual functions", but "polymorphism" just means
> having one piece of code operate on different kinds of data. Templates
> are a kind of polymorphism, and that's what I'm referring to.
>
In the general C++ community, there is a sharp distinction between
run-time and compile-time polymorphism, due to the 'zero-overhead'
philosophy of C++, (ie. you don't pay for what you don't absolutely
_have_ to pay for). Therefore, unqualified use of the term
'polymorphism' is normally assumed to mean run-time polymophism (simply
because that was added to C++ first), although you are correct in your
usage of it otherwise. The 'safe' term to use in this case is 'generic'
code. Just FYI.

> My question, rephrased, is just "why use templates?" What does this
> templated structure give you that you wouldn't get from just having a
> bunch of unrelated classes (rgb_color, cymk_color, etc.)? How would a
> programmer take advantage of your templated structure?
>
The idea is to write code that can operate on graphical data without
assuming anything about the representation of said data. To do that
efficiently in C++ is practicly a description of what templates are for.

> In other words, can you give an example of how you see this library
> being used, in a way that wouldn't be possible (or easy) without
> templates?

consider a function that *someone* will need to use, that doesn't depend
on the representation of the data:

// where GraphContext is a TwoDimContainer with a value_type
// convertable to PixelRepr
template <class PixelRepr, class GraphContext>
void resample(const GraphContext& from, GraphContext& to)
{ // ...
                // Dumb aliasing resampling
                to[x][y] = from[x*x_scale][y*y_scale];
        // ...
}

<snip gargantuan quote placed below replies>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk