You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置clang-format使注释与左大括号保持同行?

解决clang-format将大括号后注释移至新行的问题

在现有clang-format配置中添加KeepCommentsWithStatements: true项即可实现需求,修改后的完整配置如下:

BasedOnStyle: Google
Language: Cpp
ColumnLimit: 120
KeepCommentsWithStatements: true

配置说明

  • KeepCommentsWithStatements: true:该配置会强制让注释与它所属的代码语句保持在同一行,避免clang-format将左大括号后的注释单独移至新行。
  • 保留BasedOnStyle: Google可以维持Google风格的其他格式化规则,比如函数、类、命名空间的大括号单独换行,循环、条件语句的大括号与主体同行等。

修改后,clang-format会输出你期望的格式化效果:

foreach(const QString& value, list_string) { /// value must be latter container inclusion type
    if (value == "c") { break; }
    qDebug() << value;
}

内容的提问来源于stack exchange,提问作者Spike Aaron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:05:02