如何告诉clang-format不要在单行上做短lambda表达式?

问题描述 投票:3回答:1
#include <iostream>

int main()
{
    auto func = []() { 
        std::cout << "hello, world" << std::endl; 
    };
    func();
}

我想用这种方式来保存我的短lambda表达式,但clang-format总是把它放在单行上。但是clang-format总是把它放在单行上。

auto func = []() { std::cout << "hello, world" << std::endl; };

我阅读了文档,但我找不到正确的设置。以下是我的配置:

BasedOnStyle:   LLVM
Language:   Cpp
AccessModifierOffset:   -4
AlignAfterOpenBracket:  Align
AlignConsecutiveAssignments:    true
AlignConsecutiveDeclarations:   true
AlignEscapedNewlines: Left
AlignOperands:  true
AlignTrailingComments:  true
AllowAllParametersOfDeclarationOnNextLine:  true
AllowShortBlocksOnASingleLine:  false
AllowShortCaseLabelsOnASingleLine:  false
AllowShortFunctionsOnASingleLine:   Empty
AllowShortIfStatementsOnASingleLine:    true
AllowShortLoopsOnASingleLine:   false
AlwaysBreakAfterDefinitionReturnType:   None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings:  false
AlwaysBreakTemplateDeclarations:    false
BinPackArguments:   false
BinPackParameters:  false
BraceWrapping:   
  AfterClass:   true
  AfterControlStatement: true   
  AfterEnum: true   
  AfterFunction:    true
  AfterNamespace:   false
  AfterObjCDeclaration: false
  AfterStruct:  true
  AfterUnion:   false
  BeforeCatch:  true
  BeforeElse:   true
  IndentBraces: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces:  Custom
BreakBeforeTernaryOperators:    true
BreakConstructorInitializersBeforeComma:    true
ColumnLimit:    120
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth:  4
ContinuationIndentWidth:    4
Cpp11BracedListStyle:   false
DerivePointerAlignment: false
DisableFormat:  false
ExperimentalAutoDetectBinPacking:   false
ForEachMacros:  [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories: 
  - Regex:  '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:   2
  - Regex:  '^(<|"(gtest|isl|json)/)'
    Priority:   3
  - Regex:  '.*'
    Priority:   1
IndentCaseLabels:   true
IndentWidth:    4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks:   true
MacroBlockBegin:    ''
MacroBlockEnd:  ''
MaxEmptyLinesToKeep:    1
NamespaceIndentation:   Inner
ObjCBlockIndentWidth:   4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList:    true
PenaltyBreakBeforeFirstCallParameter:   19
PenaltyBreakComment:    300
PenaltyBreakFirstLessLess:  120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine:  60
PointerAlignment:   Right
ReflowComments: true
SortIncludes:   true
SortUsingDeclarations: true
SpaceAfterCStyleCast:   false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens:  ControlStatements
SpaceInEmptyParentheses:    false
SpacesBeforeTrailingComments:   1
SpacesInAngles: false
SpacesInContainerLiterals:  true
SpacesInCStyleCastParentheses:  false
SpacesInParentheses:    false
SpacesInSquareBrackets: false
Standard:   Cpp11
TabWidth:   4
UseTab: Never

我的clang-format版本是7.0.0,如果lambda表达式有两行以上,它就能很好地工作,但当它只有一行时,clang-format总是把它变成一行。但是当它只有一行的时候,clang-format总是把它变成一行。有时一个短函数只有一行,clang-format也会把它写成一行。

clang-format
1个回答
0
投票

我不知道这个选项是什么时候添加的,至少从10.0版本开始有了这个选项。AllowShortLambdasOnASingleLine. 见 正式文件.

在你的情况下,你需要选项的值是 InlineEmptyNone.

© www.soinside.com 2019 - 2024. All rights reserved.