Code critique

This category is for posts where I do a critique of published C++ code.

For reasons that I cannot fathom, templates terrify many C++ programmers. Not just beginners; hang around any sizable C++ community for any length of time and you will inevitably see someone who is otherwise quite competent going to irrational lengths to avoid writing a template. This fear baffles me, because templates are not really any harder to write than non-templates – in fact, in many cases you can simply replace the hard-coded types with T, add the template declaration, and you’re done. This refusal to simply go generic when it is obviously the way to go can make an otherwise potentially useful bit of code almost entirely useless. Continue Reading

One can generally count on Stack Overflow for excellent, high-quality C++ advice. Several C++ experts regularly contribute (I see Howard Hinnant frequently, for example). But Stack Overflow won’t always give you good answers. Here’s a case where a request for a simple solution generates 10+ responses with code ranging from ~10-30 lines – and in one case over 60 lines spread over two files! – that use macros, templates, proxy objects, and in more than a few cases have surprising and likely annoying side effects… for something that could have been solved with a single character. Yes. One… single… character. Continue Reading