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

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.


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.


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.