Search code examples
c++clang-format

In clang-format how can i ensure that there is a line break after a template declaration


As written in the clang-format documentation I set the BreakTemplateDeclarations Option to "Yes", but when format my Code it still gets put on the same line. e.g.

template<typename type> class Class{};

instead of

template<typename type>
class Class{};

Is there anything I´m overlooking?

I tried the BreakTemplateDeclarations Option and I also tried the (deprecated) AlwaysBreakTemplateDeclarations Option but none made a difference.

This is my current .clang-format File:

---
BasedOnStyle: WebKit
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Left
AlignOperands: AlignAfterOperator
AlignConsecutiveMacros: None
AlignEscapedNewlines: DontAlign
AlwaysBreakTemplateDeclarations: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: Empty
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Attach
BreakBeforeConceptDeclarations: Always
BreakTemplateDeclarations: Yes
ColumnLimit: 120
Cpp11BracedListStyle: true
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: Always
FixNamespaceComments: true
IncludeBlocks: Regroup
IndentAccessModifiers: true
IndentCaseLabels: true
IndentExternBlock: Indent
IndentWidth: 4
InsertBraces: true
IntegerLiteralSeperator:
  Binary: -1
  Decimal: 3
  DecimalMinDigits: 5
  Hex: 2
  HexMinDigits: 6
JavaScriptQuotes: Double
KeepEmptyLinesAtEOF: false
KeepEmptyLinesAtTheStartOfBlocks: false
LineEnding: LF
MaxEmptyLinesToKeep: 1
PackConstructorInitializers: NextLine
NamespaceIndentation: None
PenaltyReturnTypeOnItsOwnLine: 200
PenaltyBreakOpenParenthesis: 200
PenaltyBreakTemplateDeclaration: 0
PenaltyIndentedWhitespace: 1
PointerAlignment: Right
ReferenceAlignment: Right
RemoveParentheses: MultipleParentheses
SeperateDefinitionBlocks: Always
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: false 
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false 
SpacesInAngles: Never
SpacesInContainerLiterals: false
SpacesInLineCommentPrefix:
  Minimum: 1
  Maximum: 1
SpacesInParens: Never
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Always
...

Solution

  • The problem is that you've specified a wrong value true for the key AlwaysBreakkTemplateDeclarations. From clang format documentation the possible values are:

    Possible values:

    • Leave : Do not change the line breaking before the declaration.
    • No : Do not force break before declaration.
    • Yes : Always break after template declaration.
    • MultiLine : Force break after template declaration only when the following declaration spans multiple lines.

    So to solve this, change true to yes.