首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Operator=后中断(以clang-格式表示)

Operator=后中断(以clang-格式表示)
EN

Stack Overflow用户
提问于 2018-12-18 15:03:24
回答 1查看 893关注 0票数 6

我在C++中使用来自LLVM 7.0.0和Windows 10的clang格式。

我有下一节课

代码语言:javascript
复制
class FooooooooooooooooooC
{
public:
   FooooooooooooooooooC() = default;
   const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) = delete;

};

运行clang格式之后,它应该如下所示

代码语言:javascript
复制
class FooooooooooooooooooC
{
public:
   FooooooooooooooooooC() = default;
   const FooooooooooooooooooC& operator=(
      const FooooooooooooooooooC& ) = delete;

};

但是实际上在运行clang格式之后,它看起来是这样的。

代码语言:javascript
复制
class FooooooooooooooooooC
{
public:
   FooooooooooooooooooC() = default;
   const FooooooooooooooooooC& 
   operator=( const FooooooooooooooooooC& ) = delete;

};

我在.clang格式中的clang-fromat设置如下

代码语言:javascript
复制
---
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,以便在之后创建一个中断?

operator=(

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-08-06 22:22:40

总之,你不能做你想做的事。

下面是如何配置clang-格式,使其在const FooooooooooooooooooC&之后不出现中断。

在决定如何拆分行时,clang使用了几个加权因素,其名称都以Penalty开头。在本例中,您希望返回类型与函数名保持在同一行上,因此需要调整PenaltyReturnTypeOnItsOwnLine。您的..clang格式中的值是60。相反,请使用:

代码语言:javascript
复制
PenaltyReturnTypeOnItsOwnLine: 200

使其值110或更大,以防止行在const FooooooooooooooooooC&返回类型后中断。我建议200与铬、谷歌和Mozilla预定义的clang格式风格相匹配。(而且,我也不知道为什么110是阈值;惩罚值相当不透明,我只是通过实验才找到这个值的。)

然而,您最终得到的结果是:

代码语言:javascript
复制
   const FooooooooooooooooooC& operator=( const FooooooooooooooooooC& ) =
      delete;

我不相信在operator=(之后有什么办法来强迫休息。如果您的类名比您的类名长4个字符,那么您将得到所要求的内容,因为在delete之前,上面的分隔将超过80个字符。

  • 上面的评论提到了ColumnLimit。即使允许增加列限制,它也只允许将operator=声明保持在一行上。它不允许您强迫它在operator=(之后拆分行。
  • 上面的评论提到了AllowAllParametersOfDeclarationOnNextLine: false。正如你所发现的,这并不能解决问题。当有多个参数时,这将影响是否将所有参数放在单独的行上的决定。但你只有一个参数。(参见文档。)

最后,请注意,我使用的是clang-格式6.0.0,与您的7.0.0相比。但是,在clang-格式上似乎没有任何不同,在这里会有任何不同:

  • ReleaseNotes.html没有提到任何重要的事情
  • 与6.0.0相比,7.0.0中有几个新的clang格式样式选项(我注意到了这一点,因为clang-格式6.0.0在.clang-格式文件中抱怨它们),但与此问题无关。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53835831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档