Case of names for template typename
Given a C++ template class (or function) definition:
template<typename T>
struct Foo {
T member;
};
-- if in a more complicated case I want to make the typename more expressive, what case naming conventions are accepted or shunned (and why). Examples:
template<typename MORE_EXPRESSIVE_NAME>
struct More {
MORE_EXPRESSIVE_NAME* 开发者_开发知识库p_;
More(MORE_EXPRESSIVE_NAME* p);
...
};
template<typename MORE_EXPRESSIVE_NAME>
More<MORE_EXPRESSIVE_NAME>::More(MORE_EXPRESSIVE_NAME* p)
: p_(p)
{ }
or
template<typename MoreExpressiveName>
struct More {
MoreExpressiveName* p_;
More(MoreExpressiveName* p);
...
};
template<typename MoreExpressiveName>
More<MORE_EXPRESSIVE_NAME>::More(MoreExpressiveName* p)
: p_(p)
{ }
or
template<typename mr_exprs_nm>
struct More {
mr_exprs_nm* p_;
More(mr_exprs_nm* p);
...
};
template<typename mr_exprs_nm>
More<mr_exprs_nm>::More(mr_exprs_nm* p)
: p_(p)
{ }
The main thing about naming convention is consistency. Whatever the convention you adopt, please keep it throughout the project, and therefore if you jump in on a project where there is already one adopted, stick to it (if there is none, rename).
That being said, ALL caps are normally reserved for macros in most of the naming conventions I have seen, so I would definitely avoid it.
I myself prefer to name template parameters like I name types or constants (depending on the kind of the parameter), for consistency. In this case, given that you use More
I would use camel case too.
As for the content of what you type, it depends:
- for small functions, I usually use the Concept name, possibly abbreviated:
FwdIt
forForwardIterator
for example, as a reminder of what the type should implement - for larger class / functions, I use either a Concept or a meaningful name (business-dependent) if possible
MORE_EXPRESSIVE_NAME looks like precompiler constant. mr_exprs_nm is not much better than Foo. I vote for MoreExpressiveName. There are various naming conventions, but in this case simple common sense helps to find the best solution.
As Matthieu M. says, when it comes to naming conventions, consistency is more important than the convention itself.
That said, here's the convention I use:
CamelCase
for structs, classes, typedefs & template parameter names. Basically, anything that looks like the name for a type is CamelCase.
template<typename MoreExpressiveName> ...
I also use CamelCase
for static const
globals
static const int MyMagicNumber = 42;
underscore_lcase
for variable & parameter names. Member variables also get a trailing_underscrore_
.
template<typename MoreExpressiveName>
More<MoreExpressiveName>::More(MoreExpressiveName* my_thing)
{
my_thing_ = my_thing;
}
ALL_CAPS
for macros.
精彩评论