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条回答
迷人小祖宗
2楼-- · 2019-03-14 11:20

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;
查看更多
The star\"
3楼-- · 2019-03-14 11:21

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.

查看更多
登录 后发表回答