Using templates when you don't really need to

Templated C++ classes are a bit nicer to use than non-templated classes, because one can define the entire class within its declaration without having to make sure that the types it uses are defined first (this check is done at instantiation time for template classes, but at parse time for non-template classes). I have found myself making classes templates when they don't really need to be just so that I don't have to define member functions outside the declaration - i.e. when CTemplate doesn't actually use T for anything, and CTemplate is just used as "typedef CTemplate C;". T may be used as the template parameter to other classes defined this way, though.

3 Responses to “Using templates when you don't really need to”

  1. Darkstar says:

    I did that too some time ago but I found 2 problems with this approach: The error messages are also moved to the instantiation location rather than the class itself so it's harder to find the "real" errors inside the class. Also IMHO it often makes "fast" reading of the code harder.

    -Darkstar

  2. [...] like calling a function declared in an external binary, such as for platform ABI calls. I go to great lengths to minimize the number of declarations in my C++ [...]

Leave a Reply