clang-format: How to keep each element of constructor's initializer list on a separate line

clang-format javascript
clang-format python
clang-format-diff
clang-format objective-c
clang-format penalty
clang-format patch
clang-format vim
clang-format public private indent

I have a C++ class like this:

class A {
public:
    A() :
        m_a(someValue1),
        m_b(someValue2),
        m_c(someValue3)
    {
    }

    // .... other class members

private:
    A m_a;
    B m_b;
    C m_c;
};

After formatting this code with clang-format I am getting:

class A {
public:
    A() :
        m_a(someValue1), m_b(someValue2), m_c(someValue3)
    {
    }

    // .... other class members

private:
    A m_a;
    B m_b;
    C m_c;
};

I.e. initializer list in constructor got formatted onto single line, until they permitted maximum line length is reached.

My question is how to tell clang-format to leave each element on its own line, like it was in my original code before formatting? I could not find any suitable parameter. I've tried to set parameter AllowShortBlocksOnASingleLine, which seemed to me the most appropriate, to the both true and false, but it had no effect on this. So can anyone suggest how to implement such formatting?

Here is my .clang-format:

BasedOnStyle: Google
AccessModifierOffset: '-4'
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: 'false'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: Left
AlignOperands: 'true'
AlignTrailingComments: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'true'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BinPackArguments: 'true'
BinPackParameters: 'true'
BreakAfterJavaFieldAnnotations: 'true'
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
SplitEmptyFunction: true
BreakBeforeInheritanceComma: 'false'
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: AfterColon
BreakStringLiterals: 'true'
ColumnLimit: '100'
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '8'
Cpp11BracedListStyle: 'true'
DerivePointerAlignment: 'false'
DisableFormat: 'false'
ExperimentalAutoDetectBinPacking: 'false'
FixNamespaceComments: 'true'
IncludeBlocks: Preserve
IndentCaseLabels: 'true'
IndentPPDirectives: None
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
JavaScriptQuotes: Double
JavaScriptWrapImports: 'true'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
NamespaceIndentation: None
PointerAlignment: Left
ReflowComments: 'false'
SortIncludes: 'true'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'true'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '2'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: Cpp11
TabWidth: '4'
UseTab: Never

UPDATE: There is option in clang-format called "ConstructorInitializerAllOnOneLineOrOnePerLine" descrined as follows: "If the constructor initializers don’t fit on a line, put each initializer on its own line". However, it still doesn't do what I want, because it only moves initializer to a new line if it doesn't fit to column limit. So looks like there is no way to force clang-format to put subsequent initializer on next line even if column limit is not reached. It would be nice if abovementioned option turned into enum with additional value that forces to put initializer to a next line even if column limit is not reached.


I am pretty sure that this is a bug / shortcoming of clang-format. The issue was already addressed in 2015, but rejected by the clang-format developers: https://reviews.llvm.org/D14484

For what it is worth, I made a simple change to clang-format that should give you, your intended behaviour: https://github.com/Nikolai-Hlubek/clang/tree/ConstructorInitializer_AlwaysBreakAfterColon

I made a push request to upstream, but I doubt that it will be accepted.

Clang-Format Style Options, When using -style=file , clang-format for each input file will try to find the If a constructor definition with a member initializer list doesn't fit on a single line, the following line unaffected #include <vector> // FOOBAR pragma: keep The last element of each list is a non-namespace name; all others are namespace names. If true, clang-format will sort using declarations. The order of using declarations is defined as follows: Split the strings by “::” and discard any initial empty strings. The last element of each list is a non-namespace name; all others are namespace names.


I personally use

BreakConstructorInitializers: BeforeComma

but other options are available. See Clang-Format Style Options, in the section BreakConstructorInitializers. It looks like your style would be AfterColon.

Clang-Format Style Options, When using -style=file , clang-format for each input file will try to find the If a constructor definition with a member initializer list doesn't fit on a single line, the following line unaffected #include <vector> // FOOBAR pragma: keep The last element of each list is a non-namespace name; all others are namespace names. Configuring Style with clang-format¶. clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the .clang-format or _clang-format file in the project directory.


I am trying to do the same thing. The best I could do was:

SpaceBeforeCtorInitializerColon: true
ConstructorInitializerIndentWidth: 4
BreakBeforeInheritanceComma: false
BreakInheritanceList: AfterColon
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
ConstructorInitializerAllOnOneLineOrOnePerLine: true

However, this will still place the list on a single line unless you break the column limit.

D14484 [clang-format] Formatting constructor initializer lists by , I am hesitant to patch clang-format as I don't want to have to maintain it so I hope this is added as an option. Or if there was a way we could add  If true, clang-format will sort using declarations. The order of using declarations is defined as follows: Split the strings by “::” and discard any initial empty strings. The last element of each list is a non-namespace name; all others are namespace names.


clang/include/clang/Format/Format.h, list doesn't fit on a. 176, /// line, allow putting all arguments onto the next line, even if 191, /// \brief If a constructor definition with a member initializer list doesn't 1141, /// #include <vector> // FOOBAR pragma: keep. 1142, /// \ The last. 1869, /// element of each list is a non-namespace name; all others are namespace. clang-format is integrated into CLion as an alternative code formatter. CLion turns it on automatically when there is a .clang-format file under the project root. Code style rules are applied as you type, including indentation, auto-completion, code generation, and refactorings. clang-format can also be enabled without a .clang-format file. In


Coding Style (OpenApoc), With this, it is highly recommended to run clang-format on all All members should be initialised in all constructors if they don't have a default constructor. possibly removing elements during loop, then use iterator and keep  These element indices are numbered sequentially starting with the first vector, continuing into the second vector. Thus, if vec1 is a 4-element vector, index 5 would refer to the second element of vec2. An index of -1 can be used to indicate that the corresponding element in the returned vector is a don’t care and can be optimized by the backend.


Coding Style, Any violation to the guide is allowed if it enhances readability. can be automated with tools such as clang-format and clang-tidy, which can help class with parameters * * Detailed description of the constructor here if needed Using a separate inline file is useful to keep the header files clean and small. Constructors (C++) 12/27/2019; 17 minutes to read +7; In this article. To customize how class members are initialized, or to invoke functions when an object of your class is created, define a constructor. A constructor has the same name as the class and no return value.


code_standards.md|MOOSE - MOOSE Framework, We use clang-format (with a customized config file) for code formatting. C++ constructor spacing is demonstrated in the bottom of the example below indent and one item per line for long (i.e. won't fit on one line) initialization list. In keeping with good auto practices, it would be really nice if we could use auto for loop  Summary: This option replaces the BreakConstructorInitializersBeforeComma option with an enum, thus introducing a mode where the colon stays on the same line as