我有兴趣为函数添加一个左大括号(但不是 if 语句和其他上下文)。例如
void foo()
{
...
}
抛开火焰大战不谈,不这样做有什么好的理由吗?尽管我对 if/else 和更小的 block 使用同行开括号,但我认为在这种情况下,更大的代码单元(函数/方法/类/结构)的可视化组织可以胜过完美的一致性。
此外,我如何让 clang-format 遵循这种风格?
最佳答案
作为documentation说,调用 clang-format
与 -style=file
, 并使用 .clang-format
放置在封闭目录中的文件以自定义样式选项。指定大括号位置的格式样式选项称为 BreakBeforeBraces
.从文档中,
BreakBeforeBraces (
BraceBreakingStyle
)The brace breaking style to use.
Possible values:
BS_Attach
(in configuration:Attach
) Always attach braces to surrounding context.BS_Linux
(in configuration:Linux
) LikeAttach
, but break before braces on function, namespace and class definitions.BS_Stroustrup
(in configuration:Stroustrup
) LikeAttach
, but break before function definitions, and ‘else’.BS_Allman
(in configuration:Allman
) Always break before braces.BS_GNU
(in configuration:GNU
) Always break before braces and add an extra level of indentation to braces of control statements, not to those of class, function or other definitions.
符合您描述的样式是
BS_Stroustrup
.将以下条目添加到您的 .clang-format
BreakBeforeBraces: Stroustrup
除了文档,clangformat.com列出所有选项,并通过示例说明其中的许多选项。
关于coding-style - 如何在打开函数的大括号之前使 clang-format 添加新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29477654/