简体   繁体   English

模板类型名的名称大小写

[英]Case of names for template typename

Given a C++ template class (or function) definition: 给定一个C ++模板类(或函数)定义:

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. 在这种情况下,假设您使用“ More我也将使用驼峰式大小写。

As for the content of what you type, it depends: 至于键入内容,则取决于:

  • for small functions, I usually use the Concept name, possibly abbreviated: FwdIt for ForwardIterator for example, as a reminder of what the type should implement 对于小型函数,我通常使用概念名称(可能缩写为: FwdIt for ForwardIterator ,以提醒类型应实现的类型
  • for larger class / functions, I use either a Concept or a meaningful name (business-dependent) if possible 对于较大的类/函数,如果可能,我使用“概念”或有意义的名称(与业务相关)

MORE_EXPRESSIVE_NAME looks like precompiler constant. MORE_EXPRESSIVE_NAME看起来像预编译器常量。 mr_exprs_nm is not much better than Foo. mr_exprs_nm并不比Foo好多少。 I vote for MoreExpressiveName. 我投票支持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. 正如Matthieu M.所说,在命名约定时,一致性比约定本身更为重要。

That said, here's the convention I use: 就是说,这是我使用的约定:

CamelCase for structs, classes, typedefs & template parameter names. CamelCase用于结构,类,typedef和模板参数名称。 Basically, anything that looks like the name for a type is CamelCase. 基本上,任何看起来像类型名称的东西都是CamelCase。

template<typename MoreExpressiveName> ...

I also use CamelCase for static const globals 我还将CamelCase用于static const全局变量

static const int MyMagicNumber = 42;

underscore_lcase for variable & parameter names. underscore_lcase表示变量和参数的名称。 Member variables also get a trailing_underscrore_ . 成员变量还获得了trailing_underscrore_

template<typename MoreExpressiveName> 
More<MoreExpressiveName>::More(MoreExpressiveName* my_thing)
{
  my_thing_ = my_thing;
}

ALL_CAPS for macros. 宏的ALL_CAPS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM