c++ - clang-format:类声明结束和命名空间关闭之间的空行

标签 c++ clang code-formatting clang-format

我使用 clang-format 来格式化我们的 C++ 代码。我想在类声明和周围命名空间的右大括号之间有一个空行,如下所示:

namespace Foo {

class Bar {
};

}

但是 clang-format 将我的代码更改为:

namespace Foo {

class Bar {
};
}

它删除了类声明和命名空间的右大括号之间的空行。

我的问题:有没有办法防止 clang-format 删除空行?

这是我当前的 clang 格式配置:

Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignEscapedNewlinesLeft: false
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [foreach, Q_FOREACH, BOOST_FOREACH]
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 4
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never

最佳答案

我找到了解决此问题的一种方法:在命名空间右大括号中添加内联注释,如下所示:

namespace my_namespace {

class MyClass {
    int n;
};

} // namespace my_namespace

关于c++ - clang-format:类声明结束和命名空间关闭之间的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33547172/

相关文章:

perl - 配置 Perl::Tidy 来处理自定义关键字

c++ - mmap 一个 10 GB 的文件并将其加载到内存中

android - C++Builder:如何在 Android 应用程序中显示重音?

c++ - 在 Qt 4.4 中使用 QMDIArea。

c++ - LTO 导致标准库崩溃

C# 正确格式化(缩进,对齐)C#

c++ - 断言与 ATLASSERT 与断言

c++ - 使用 block 在 C++ 中避免在初始化期间复制变量

c++ - OSX 10.10 CMake 3.0.2 和 clang 找不到本地 header

Eclipse 格式化程序在空注释行中添加空格