NL.17:使用K&R风格派生的布局
This is the original C and C++ layout. It preserves vertical space well. It distinguishes different language constructs (such as functions and classes) well.
这是原始的C和C ++布局。它很好地保留了垂直空间。它很好地区分了不同的语言构造(例如函数和类)。
Note(注意)
In the context of C++, this style is often called "Stroustrup".
在C ++的上下文中,此样式通常称为“斯特劳斯特卢普”。
This is a recommendation for when you have no constraints or better ideas. This rule was added after many requests for guidance.
当您没有约束或更好的选择时,可以考虑这个个建议。此规则可以在许多准则之后适用。
Example(示例)
struct Cable {
int x;
// ...
};
double foo(int x)
{
if (0 < x) {
// ...
}
switch (x) {
case 0:
// ...
break;
case amazing:
// ...
break;
default:
// ...
break;
}
if (0 < x)
++x;
if (x < 0)
something();
else
something_else();
return some_value;
}
Note the space between if and (
注意if和(之间的空格
Note(注意)
Use separate lines for each statement, the branches of an if, and the body of a for.
对于每个语句,if的分支和for的正文使用单独的行。
Note(注意)
The { for a class and a struct is not on a separate line, but the { for a function is.
类和结构的{不在单独的行上,而函数的{是。
Note(注意)
Capitalize the names of your user-defined types to distinguish them from standards-library types.
大写用户定义类型的名称,以将其与标准库类型区分开。
Note(注意)
Do not capitalize function names.
不要大写函数名称。
Enforcement(实施建议)
If you want enforcement, use an IDE to reformat.
如果要强制执行,请使用IDE重新格式化。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl17-use-kr-derived-layout