Leigh Johnston said:
That was the point I was making: if a single object is responsible for
drawing all the shapes then for it to benefit from this dubious
"optimization" it needs to be aware of all the different sub-class types
in order to correctly instantiate this useless function template.
I really cannot that single object you are talking about.
For example if you want to draw all pixels of an Image, you could use the
same rendering algorithm for your collection of pixels and it will be
nicely optimized due to inlining.
I wouldn't even consider rendering pixel like this with runtime
polymorphism.
Consider the following example:
class Drawable
{
public:
virtual void draw(/*...*/) = 0;
};
template <typename Iterator>
void drawSequence(Iterator begin, Iterator end)
{
static_assert(std::is_base_of<Drawable,
std::iterator_traits<Iterator>::value_type>::value, "Must implement
Drawable");
for (Itetator it = begin; it != end; ++it)
it->draw(/*...*/);
}
class Pixel : public Drawable
{
public:
virtual void draw(/*...*/) final { /* simple */ }
};
class Image : public Drawable
{
public:
virtual void draw(/*...*/) final
{
// can be optimized, value type is statically known
drawSequence(pixels.begin(), pixels.end());
}
private:
std::vector<Pixel> pixels;
};
class Path : public Drawable
{
public:
virtual void draw(/*...*/) final { /* complex */ }
};
typedef boost::indirect_iterator<std::vector<Drawable*>, Drawable> ItD;
int main(/*.../)
{
std::vector<Drawable*> objects;
objects.push_back(new Image);
objects.push_back(new Path);
// Normal runtime polymorphism
draw_sequence(ItD(objects.begin()), ItD(objects.end()));
}
Like I said earlier C++ templates are great I just feel they are being
misused in this case. This is a classic example of premature optimization.
I think in a program or library that deals with graphics, this an important
design decision, not premature optimization.
Tobi