![](/img/trans.png)
[英]clang-format: How to keep each element of constructor's initializer list on a separate line
[英]Prevent clang-format from breaking after brace in constructor's initializer list
我有一個 class 構造函數定義,它初始化了幾個字符串。
基本上,我希望 clang-tidy 在格式化后給我這個:
A::A()
: index{1}, string_one{"some pretty string"},
string_two{"another pretty string"} {
}
但是,它給了我這個:
A::A()
: index{1}, string_one{"some pretty string"}, string_two{
"another pretty string"} {
}
我希望 clang-format 盡量避免在參數名稱(例如string_two
)與其構造函數 arguments 之間中斷。相反,我希望它將參數及其構造函數 arguments 一起移動到一個新行。
有沒有辦法讓 clang-format 給我以前的 output? 如果我使用括號而不是括號來初始化參數,它將產生我期望的 output。
這些是我的.clang-format
文件的內容:
---
BasedOnStyle: LLVM
Language: Cpp
Standard: c++17
TabWidth: 4
IndentWidth: 4
AccessModifierOffset: -4
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
NamespaceIndentation: All
ColumnLimit: 80
BinPackArguments: true
BinPackParameters: true
AllowShortFunctionsOnASingleLine: None
...
使用 clang-format-14,如果將這些添加到您的 .clang-format 文件中,您可以獲得所需的格式:
PackConstructorInitializers: Never
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
請記住,設置BreakBeforeBraces: Custom
很可能會對您的代碼進行額外的修改,因為您不再從 LLVM 繼承該行為。
更多信息請查閱官方文檔: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.