What are some explicit and official guidelines or standards for template parameter naming in C++? I've done something like this in the past:

template<typename TContainer, typename TKey>
void AddKeyToMap(TContainer& the_map, TKey const& the_key);

I've also seen names like container_t and key_t. I'd also accept advice against standardization here, if it makes sense.

  • Using xxx_t intrudes on POSIX, which reserves all types ending in _t for its own use. So if you live in a POSIX world, don't do that. – Blrfl Sep 13 '14 at 14:32

As with any question of naming, this is more about consistency in an arbitrary choice than objective measures. However, there is precedent toward specific conventions. The Google C++ Style Guide says nothing about type parameters, but since they constitute type names, the usual rules apply:

Type names start with a capital letter and have a capital letter for each new word, with no underscores: MyExcitingClass, MyExcitingEnum.

The names of all types — classes, structs, typedefs, and enums — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores. For example:

// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...

// typedefs
typedef hash_map<UrlTableProperties *, string> PropertiesMap;

// enums
enum UrlTableErrors { ...

According to this standard, you would write:

template<typename Container, typename Key>
void AddKeyToMap(Container& the_map, Key const& the_key);

The T prefix could be construed as Hungarian notation, specifically systems Hungarian in which the identifier includes an encoding of its type—here, typename. This is widely seen as redundant and potentially harmful, including by Stroustrup:

I don’t like naming a variable after its type; what do I like and recommend? Name a variable (function, type, whatever) based on what it is or does. Choose meaningful name; that is, choose names that will help people understand your program.

I agree that in some cases, building type hints into variable names can be helpful, but in general, and especially as software evolves, this becomes a maintenance hazard and a serious detriment to good code.

It boils down to the semantics you have in mind when you write the program. If you think of type parameters as types, use the same convention you would for type names. If you think of them as parameters or variables, use the same convention you would for parameter or variable names. It’s as simple as that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.