How to auto indent a C++ class with 4 spaces using clang-format?

As near as I can tell, clang-format offers no option for indenting function contents differently from non-access-modifier class contents. That is, consider the following code:

class A {
  public:
    void foo() {}
}

void bar() {
    int a;
}

In this code, the line “void foo() {}” will always be indented the same amount as “int a;” by clang-format.

The closest thing to the style you seem to want that is available would come from not indenting the access modifiers, e.g.:

class A {
public:
    void foo() {}
}

void bar() {
    int a;
}

This is done, for example, by the WebKit, Mozilla, and LLVM styles. It’s achieved by setting:

IndentWidth: 4
AccessModifierOffset: -4

Leave a Comment