2

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.

closed as primarily opinion-based by user40980, gnat, Bart van Ingen Schenau, GlenH7, ratchet freak Sep 16 '14 at 15:35

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
3

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.