views:

548

answers:

8

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?

+2  A: 

Generally, the traditional way is to use T if there is only one type param. If there is more, use T as prefix, e.g. TAtom. "T" prefix helps to instantly see it's type parameter. Using TAtom for a single type parameter is also valid.

ya23
+11  A: 

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.

JaredPar
...and could also confuse the compiler. Try #include <windows.h> template <class ATOM> class Foo {};
Andreas Magnusson
I don't use T as prefix because it is the same prefix used by some products from "Borland" and for me it is a bit confusing.
Ismael
A: 

If I have class with one type parameter I am using name T. Also it mean that all operartion in this class are working with T.

If I have few parameter I'm naming as in your explamle AtomT, BioT...
If template parameter is not type of object with which we working in clas e.g. strategy, comaparator or functor, I'm using name without T e.g. ThreadStrategy, Compare.

Sometimes for avoid mixing styles I'm making typedefs in class:
typedef T value_type;

--
Boost naming convention (http://www.boost.org/development/requirements.html#Naming_consistency) says about template parameters next:
Template parameter names begin with an uppercase letter.

bb
+1  A: 

You shouldn't use a special naming convention for templates, just use the same convention as for any other of that type (as for classes or variables). It shouldn't matter in the code whether you are working with template types/values or normal ones.

Patrick Daryll Glandien
True for names of parameterised types, Foo<int>, but the question was about type parameter names, the T in template Foo<T>. So it's not a type, it's a parameter name.
Pete Kirkham
+1  A: 

I try to follow the notion my compiler vendor uses: it's not too short and not too verbose. And helps me read the error messages I get with standard templates. (Which is another reason why I switched from const T& to T const&). Something like:

template <class Ty, class Container>
class my_algo { ...

where my compiler will typically use:

template <class _Ty, class _Container>
class std_algo { ...
dirkgently
I think you already know this, but for anyone else reading this, note that he does not use a leading underscore. Those are RESERVED for the compiler. That is, standard library templates are allowed to use _Ty. Users are not. Ty is legal, since it does not start with an underscore.
jalf
@jalf: +1. Excellent catch :) I was being plain lazy to touch upon this little known but `can-come-back-and-bite-you-later` point.
dirkgently
Quod licet Iovi, non licet bovi.
Longum iter est per praecepta, breve et efficax per exempla.
dirkgently
+2  A: 

I use convention TName for the template parameter and NameT for stored template parameter.

template <typename TFirst, typename TSecond>
class Templated
{
    typedef TFirst FirstT;
    typedef TSecond SecondT;
}

typedef Templated<int, std::string> MyTemplated;
...
const MyTemplated::FirstT size;
Mykola Golubyev
Haha, that's funny.
Mykola, what's the point...
What's the point of storing templated types? They are different. I just showed how I names those stored types. Kind of my own convention.
Mykola Golubyev
Right. It wasn't clear before the edit.
A: 

I follow the same general conventions naming template parameter typenames as I follow naming classes & structs, which is to capitalize the first letter or each word, like so:

class MyGizmo
{
};

struct Thingy
{
};

class TPSReport 
{
};


template<class ValType> ...

template<typename Number> ...
John Dibling
A: 

At our shop, we use HungF##ngarian notation. Template arguments are just arguments like all others, except they're not a const, nor a variable, but a type.

template< typename at_Container, typename at_Functor > 
at_Functor& foreach( const at_Container& ac_Cont, at_Functor& av_Func ) {
    return std::foreach( ac_Cont.begin(), ac_Cont.end(), av_Func );
}

The prefix describes the type, while the name is meant to say something of the role the argument plays in the context of the defined function.

xtofl