c++ - 在 operator=( 以 clang 格式后中断
问题描述
我在 C++ 中使用来自 LLVM 7.0.0 和 Windows 10 的 clang 格式。
我有以下课程
class FooooooooooooooooooC
{
public:
FooooooooooooooooooC() = default;
const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) = delete;
};
运行 clang-format 后应该是这样的
class FooooooooooooooooooC
{
public:
FooooooooooooooooooC() = default;
const FooooooooooooooooooC& operator=(
const FooooooooooooooooooC& ) = delete;
};
但实际上运行 clang-format 后它看起来像这样
class FooooooooooooooooooC
{
public:
FooooooooooooooooooC() = default;
const FooooooooooooooooooC&
operator=( const FooooooooooooooooooC& ) = delete;
};
我的 .clang 格式的 clang-fromat 设置是
---
AccessModifierOffset: -3
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterJavaFieldAnnotations: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
CommentPragmas: '^ IWYU pragma:'
ColumnLimit: 80
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 3
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 3
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 1000000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: true
Standard: Cpp11
TabWidth: 3
UseTab: Never
...
有人知道如何配置 clang-fromat 以在之后创建中断
运算符=(
谢谢!
解决方案
总之,你不能完全做你想做的事。
以下是如何配置 clang- format在const FooooooooooooooooooC&
.
在决定如何拆分线路时,clang-format 使用了几个权重因子,它们的名称都以Penalty
. 在这种情况下,您希望返回类型与函数名称保持在同一行,因此您需要调整PenaltyReturnTypeOnItsOwnLine
. 您的 .clang 格式中的值为60
. 相反,使用:
PenaltyReturnTypeOnItsOwnLine: 200
将其设置为任何值或更大,以防止在返回类型110
后行被破坏。const FooooooooooooooooooC&
我建议200
匹配 Chromium、Google 和 Mozilla 的预定义 clang 格式样式。(另外,我不知道为什么110
是阈值;惩罚值是相当不透明的,我只是通过实验发现了这个值。)
但是,您最终得到的是:
const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) =
delete;
我不相信有任何方法可以在之后强制休息operator=(
。如果您的班级名称长4 个字符,那么您会得到您想要的,因为上面的拆分delete
会超过 80 个字符。
上面的评论提到
ColumnLimit
。即使您被允许增加列限制,它也只允许您将operator=
声明保留在一行上。它不允许你强迫它在operator=(
.上面的评论提到
AllowAllParametersOfDeclarationOnNextLine: false
。正如您所发现的,这并不能解决问题。当有多个参数时,这会影响是否将所有参数放在单独的行上的决定。但是你只有一个参数。(请参阅文档。)
最后,需要注意的是,与您的 7.0.0 相比,我使用的是 clang-format 6.0.0。但是 clang-format 似乎没有任何差异,这会在这里产生任何影响:
- ReleaseNotes.html没有提到任何重要的事情
- 与 6.0.0 相比,7.0.0 中有几个新的 clang-format 样式选项(我注意到这是因为 clang-format 6.0.0 在您的 .clang-format 文件中抱怨它们),但没有一个与此问题相关。
推荐阅读
- javascript - 通过字符串调用外部库函数
- excel - 使用 VBA 和 selenium 抓取时从部分 id 中获取全部价值
- google-analytics - 通过性能最高的 GTM 或 Gtag 部署 Optimize
- android - 游戏开发:如何为速度添加加速度?
- google-sheets - 知道单元格是否在任何公式中使用
- angular - 无法使用 -> [routerLink]=" '/profile/ ' + currentUser.id" 访问用户组件
- xml - 使用 XSLT 基于特定键值合并 XML 节点
- php - PHP 5.6.10 - preg_match():编译失败:偏移量 100 处的字符类范围无效
- dask - 将延迟对象列表转换为 Dask 数组
- python - Python在元组中查找键