coding-style - 如何在打开函数的大括号之前使 clang-format 添加新行?

标签 coding-style clang clang-format

我有兴趣为函数添加一个左大括号(但不是 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) Like Attach, but break before braces on function, namespace and class definitions.
  • BS_Stroustrup (in configuration: Stroustrup) Like Attach, 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/

相关文章:

c# - 我应该将每个类放在单独的文件中吗?

java - 使用 if(0 == foo()) 而不是 (foo() == 0) 有什么好处?

c++ - 不能在 C++ 文件中包含标准库

c++ - 在Eclipse IDE中运行C/C++ clang-format 插件需要做哪些设置?

c++ - Clang 格式不使用 .clang 格式文件 ubuntu(无错误)

ruby - 在类里面写 Rescue 还是不写?

c++ - MySQL 和 C 应用程序

c++ - 从 native C/C++ 生成并运行 LLVM 代码

runtime-error - 如何初始化 LLVM 的外部符号化器?

c++ - C++ 中结构数组的 clang 格式奇怪缩进