3 ms·
I like the idea of this article, but I'm not sure that he chose a very good example. He's gone from showing a fragile run-time solution in C, to a slightly clu
by nkurz 16y ago
I like the idea of this article, but I'm not sure that he chose a very good example. He's gone from showing a fragile run-time solution in C, to a slightly clunky compile-time approach in C++. But if this is the whole goal, why not just create a separate function for each of the three pixel types in C?
For me, the interesting example would be showing how elegantly one can do the run-time solution in C++. In C, I'd probably give the structs a common header and add a rasterize function pointer called from a common loop. What's a more elegant way to do it in C++?
[edit: The author does point out elsewhere that in this case he really does want a compile-time solution, although I'm not sure I understand his reasoning: http://www.reddit.com/r/programming/comments/fzwlh/for_c_programmers_that_hate_c/c1jx5ml http://www.reddit.com/r/programming/comments/fzwlh/for_c_pro... ]
- corysama 16y agoThis example becomes much stronger when you have more than one rasterize function. Writing 3 separate functions for 3 pixel types is reasonable. Writing 9 functions for 3 pixel types * 3 rasterization styles is repetitive and error-prone. Writing 27 separate functions because you added 3 blend modes is very, very bad. At that point in C, you either start writing "creative" preprocessor macros, write a custom code generator or give up and eat the performance penalty of the run-time solution. A rasterization function needs to perform on the order of 1024x768x30 calls per second on the low end. Compared to inlinable static function calls, function pointer overhead becomes a big deal at that scale. "Creative" template abuse can be much worse than "creative" preprocessor abuse. However, elegant application of templates, such as policy-based design, can solve code permutation situations without repetition or performance problems.