[bind10-dev] Should We Use C++ Templates?

Stephen Morris stephen at isc.org
Mon Jul 19 15:21:18 UTC 2010


At the last BIND-10 teleconference I was asked to review the base_n code, and to comment on whether the style - using C++ templates - is reasonable or too tricky to share.

On the general point of templates, we can't ignore them.  Templates are part of the C++ language, and at their simplest allow the programmer to write a single piece of code to handle cases that would otherwise require multiple blocks of similar code or the creation of an artificial class hierarchy to factor out similar functionality.  They also provide a type-safe form of macro and can improve run-time efficiency at the typical expense of longer compile times.

In practice I think that it is not so much that templates are complex that is the problem (although they can be), it is that:

a) The definitions can be extremely wordy and obscure.  This makes code difficult to read.
b) The template arguments can be littered with bookkeeping arguments (such as allocators, type tags etc.).
b) To be useful, templated classes are likely to be derived from, or use in their template arguments, template classes from the Boost or STL libraries.  In many cases the documentation for these is - how shall we say - not as clear as it could be, and to understand the class operation requires reading and understanding the header files.

In short, trying to understand a poorly-documented template class can take a lot of time and it is this that make maintenance difficult.

The operative phrase here is "poorly-documented".  If a template class is well-documented it should be no harder to understand than any other class.  So I think that if we want to use template and template meta-programming within BIND-10, we need to put more weight on how well a class is documented.  It is subjective, but when reviewing such classes, reviewers should ask themselves whether they felt that they took an excessively long time to understand the code.  If so, it needs better documentation.

Stephen




More information about the bind10-dev mailing list