【问题标题】:clang-format: how to change behavior of line breakclang-format:如何更改换行符的行为
【发布时间】:2022-01-14 01:40:56
【问题描述】:

使用 clang-format,我想要的结果有点像这样:

  value = new MyClass(variable1, variable2, mystring + "test",
                      another_variable);

但我不想要下面的结果:

  value =
      new MyClass(variable1, variable2, mystring + "test", another_variable);

我该怎么办?

【问题讨论】:

    标签: c++ clang-format


    【解决方案1】:

    您可以在.clang-format 文件 (How do I specify a clang-format file?) 中尝试以下参数:

    AlignOperands: true
    PenaltyBreakAssignment: 21
    PenaltyBreakBeforeFirstCallParameter: 1
    
    • AlignOperands:你想在换行符之后对齐操作数(当你在下面对齐another_variable
    • PenaltyBreakAssignment: 21 因为是适合你情况的最短值(你有一个操场here
    • PenaltyBreakBeforeFirstCallParameter:正如documentation 描述的那样:在调用后中断函数调用的惩罚(.,所以它应该大于0

    您可以在clang-formatIn clang-format, what do the penalties do? 上了解更多关于惩罚的信息

    另一个相关且有见地的答案:Clang-format: How to avoid new line breaks


    PS:考虑在 C++ 中使用智能指针 ;)

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2020-06-06
      相关资源
      最近更新 更多