将单行方法的大括号与 clang 格式对齐

Adi*_*vit 5 c++ clang-format

假设我有以下 C++ 代码:

class Foo
{
   auto foo() { return 0; } // foo
   auto foofoo() const { return 1000; } // foofoo
   int bar() const { return 42; } // bar
};
Run Code Online (Sandbox Code Playgroud)

如何获得用于对齐大括号(以及注释)的 clang 格式:

class Foo
{
   auto foo()          { return 0;    } // foo
   auto foofoo() const { return 1000; } // foofoo
   int  bar()    const { return 42;   } // bar
   //            ^-----^--------------^  ALIGNED
};
Run Code Online (Sandbox Code Playgroud)

(实际上,对齐连续的方法 ( const) 限定符也很好)。

小智 0

BasedOnStyle: LLVM
AlignAfterOpenBracket: Align
AlignEscapedNewlines: Align
AlignConsecutiveAssignments: true
Run Code Online (Sandbox Code Playgroud)

通过将此 .clang-format 文件放置在项目的根目录中或将其添加到 IDE 的 clang-format 配置中,您可以将所需的格式应用于代码。