Clang-format: Use Cpp11BracedListStyle with AlignArrayOfStructures

Hello,

I use clang-format-15 and I want to use the nice feature AlignArrayOfStructures: Left but I’m not getting the epected result. For example, I would expect this code:

struct A
{
    int a, b;
};

A a1 = { 0, 1 };

std::array<A, 5> test { { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 } };

To be formatted like that:

struct A
{
    int a, b;
};

A a1 = { 0, 1 };

std::array<A, 10> test {
    { 1, 2 },
    { 1, 2 },
    { 1, 2 },
    { 1, 2 },
    { 1, 2 }
};

But I obtain:

struct A
{
    int a, b;
};

A a1 = { 0, 1 };

std::array<A, 10> test {
    {1,  2},
    { 1, 2},
    { 1, 2},
    { 1, 2},
    { 1, 2}
};

With the following .clang-format :

---
BasedOnStyle: Microsoft

# If the function declaration doesn't fit on a line, do not allow putting all parameters of a
# function declaration onto the next line.
AllowAllArgumentsOnNextLine: 'false'
# If the function declaration's parameters don't fit on a line, put each paramater on its own line.
BinPackArguments: 'false'

# If the function call doesn't fit on a line, do not allow putting all arguments of a
# function call onto the next line.
AllowAllParametersOfDeclarationOnNextLine: 'false'
# If the function call's arguments don't fit on a line, put each argument on its own line.
BinPackParameters: 'false'

# Put all constructor initializers on the current line if they fit. Otherwise, put each one on its
# own line. 
PackConstructorInitializers: Never
# In constructor initializer, put the comma at the beginning of the line.
BreakConstructorInitializers: BeforeComma

# In class inheritance, put the comma at the beginning of the line if list of inherited classes
# don't fit in a line.
BreakInheritanceList: BeforeComma

# Put template declaration on its own line.
AlwaysBreakTemplateDeclarations: 'Yes'
# Put template declaration for concept on its own line.
BreakBeforeConceptDeclarations: Always

# Put binary operators at the beginning of the line if expressions don't fit on a line.
BreakBeforeBinaryOperators: NonAssignment
# Horizontally align operands of binary and ternary expressions such as the operator is un-indented
# so that the wrapped operand is aligned with the operand on the first line.
AlignOperands: AlignAfterOperator

# Put spaces inside brace initialisation syntax.
Cpp11BracedListStyle: 'false'

# Put a space before brace initialisation syntax.
SpaceBeforeCpp11BracedList: 'true'

# Indent case labels inside a switch statement.
IndentCaseLabels: 'true'

# Indent preprocessor directives before #.
IndentPPDirectives: BeforeHash

# Aling array of structures to the left.
AlignArrayOfStructures: Left

QualifierAlignment: Custom
QualifierOrder: ['inline', 'static', 'constexpr', 'const', 'volatile', 'type', 'restrict']

ColumnLimit: '100'
UseTab: Never
IndentWidth: '4'
DeriveLineEnding: 'false'
UseCRLF: 'false'
ReflowComments: 'false'
SortIncludes: Never
# Align class visibility modifiers with the class keyword.
AccessModifierOffset: -4

# Add a 'namespace Foo' on closing brace automatically.
FixNamespaceComments: 'true'
# Remove empty lines at the beggining of a block.
KeepEmptyLinesAtTheStartOfBlocks: 'false'
...

Do you have an idea why Cpp11BracedListStyle does not seem to work inside aligned array of structures ?

1 Like