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

如何配置clang-format实现多行模板参数的缩进排版?

如何用clang-format v20.0.0实现嵌套模板参数的缩进排版?

我希望将嵌套模板参数按如下格式缩进排版:

// clang-format off
struct Pretty {
    std::vector<
        std::pair<
            size_t,
            std::vector<float>
        >
    > value;

    size_t destinationID;
};
// clang-format on

但使用clang-format v20.0.0时,得到的排版效果不符合预期:

struct Ugly {
    std::vector<
    std::pair<
    size_t,
    std::vector<float>
    >>
    value;

    size_t destinationID;
};

请问是否有方法配置clang-format以达到预期的排版效果?


可以通过调整clang-format的以下配置项实现预期效果,将这些配置添加到你的.clang-format文件中:

IndentWidth: 4
ContinuationIndentWidth: 4
AlignAfterOpenBracket: Align
TemplateClosingAngles: AlwaysBreak

各配置项的作用:

  • IndentWidth: 设置基础缩进单位为4个空格,匹配预期的代码缩进量
  • ContinuationIndentWidth: 控制换行后内容的缩进宽度,设为和IndentWidth相同的值,保证嵌套模板参数能逐层缩进
  • AlignAfterOpenBracket: 确保模板开角括号<后的内容对齐缩进
  • TemplateClosingAngles: 强制每个模板闭合角括号>单独换行,避免多个>>挤在同一行

如果存在ColumnLimit配置,建议将其设置为足够大的值,避免因列宽限制导致排版不符合预期。


内容的提问来源于stack exchange,提问作者Caglayan Dokme

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 14:21:04