Naming conventions for template types?

2019-03-14 10:52发布

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?

8条回答
Emotional °昔
2楼-- · 2019-03-14 10:56

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.

查看更多
Root(大扎)
3楼-- · 2019-03-14 10:59

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.

查看更多
Root(大扎)
4楼-- · 2019-03-14 11:07

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 { ...
查看更多
聊天终结者
5楼-- · 2019-03-14 11:11

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> ...
查看更多
Anthone
6楼-- · 2019-03-14 11:12

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.

查看更多
Deceive 欺骗
7楼-- · 2019-03-14 11:14

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.

查看更多
登录 后发表回答