we’re defining some C++ coding style guidelines based on these guidelines. Number 8 says “Names representing template types should be a single uppercase letter.” Explanation: “Common practice in the C++ development community. This makes template names stand out relative to all other names used.”
Is it really that common? I agree that it makes a lot of sense to have template<class T> class vector {...}. But what if I have several template Arguments? I don’t think that <class A, class B> is easier to understand than <class AllocationPolicy, class ThreadingPolicy>.
Do you agree that there are often cases when the given rule should not be applied (which is allowed according to 1…)?
Thanks for you thoughts!
I don’t really agree with that naming convention. For me, template parameters representing a fully generic type make sense as a simple letter –this is a type
T, I don’t really care which. But if there is any requirements on the type, it makes sense to produce a name that identifies what the template argument is:template <typename T, typename Allocator> struct container— the first type is generic, anyTwill fit, but the second one is an allocator.It is no different to functions, where you do not want your parameters to be called
a,b… in order or appearance, but rather produce meaningful names as part of the documentation. Also, if developing a style guide, I would consider requiring local typedefs of the template arguments in the case of template classes together with static asserts on the requirements for the types (whenever possible). That is, at least until concepts provide that functionality.I consider the STL to be a good example of library and you can see that generating typedefs help in reading the code (from g++ 4.2 stl):
Classes:
And functions:
All of their names are prefixed by
_and either a capital letter or a second_, which are the names reserved for the implementation, and they prefer_Tpover_T, but at the end you can see that whenever a type is generic it is called_Tp,_Tp1…, while when it has some specific requirements attached, a more sensible name is chosen.There is a fine line in between them, as for example in
std::vector, there are actual requirements on the generic type:_Tpmust be assignable, but at the end of the day, it is a mostly generic type. Calling it_Assignablewould be weird in some containers where there are more than one requirement.