mercredi 26 août 2015

C++ templates testing

This must be a commonplace question, but I do not find it. Could you point me in the right direction or describe what you do to ensure your C++ templates are all tested.

In the new project, all code must be tested with unit tests, among other measures like code reviews. Procedures are now developed that ensure no code is going untested.

For C++ templates, I see two possibilities:

  1. Disallow templates, add a check to the continuous integration system that does a "grep template" on all sources and bails out if it finds an occurrence. I do not want to go in that direction.
  2. Require explicit template instantiation. Non-Inline parts of template classes and functions will have to be implemented in source files, not headers, and explicitly instantiated. Review will check that all template classes have non-inlined constructors defined in source files, and that template functions are non-inlined and also defined in source files. The end of the source files with the template definitions will have a section where all template instantiations are done. This list can then be checked against the unit tests by review to ensure that all instantiations are covered by tests.

A problem with solution 2 is that the template source file will have to include header files with type definitions of the clients. This feels like an inversion of dependencies.

The project has "core" library parts that are used by various, specialized modules. The library headers will live in a central place to be included by all modules.

If the library implements part of its functionality as templates, and these are used by specialized modules, then the library source file will have to include some specialized headers of the modules if some classes defined there are used as template parameters.

This is possible, but it feels like a mess. How do you solve this?

Aucun commentaire:

Enregistrer un commentaire