c++templatesnaming-conventions

Naming conventions for template types?


Traditionally, the names of template types are just a single upper-case letter:

template<class A, class B, class C>
class Foo {}; 

But I hesitate to do this because it's non-descriptive and hard therefore to read. So, wouldn't something like this be better:

template<class AtomT, class BioT, class ChemT>
class Foo {}; 

I also tend to think the following would not be a bad idea:

template<class ATOM, class BIO, class CHEM>
class Foo {}; 

It makes them stand out (and also, it's upper-case letters again). What's your opinion?


Solution

  • For C++ templates I have a couple of patterns

    If there is just a single template parameter, I name it T (or U,V for nested templates).

    When there are multiple parameters and the use is not immediately obvious then I use descriptive names prefixed with T. For example, TKey, TValue, TIdentifiier, etc ... This makes the parameters fairly easy to spot throughout the template usage.

    I would avoid the all upper case version though. Most people use all upper case identifiers in C/C++ to represent a macro definition. Repeating that pattern for a template parameter is likely to confuse people down the road.