paperbot · PL 论文追踪

全部论文 待读 收藏

P2 Fast Constraint Synthesis for C++ Function Templates

C++ 函数模板的快速约束合成
OOPSLA 9(OOPSLA1)2025匹配度 2/10 · 该论文属于经典程序语言领域的静态分析与程序合成,用于自动合成 C++ 模板约束,但未涉及机器学习系统、Agentic 程序、LLM/Agent 辅助静态分析、浮点程序分析或贝叶斯程序分析等用户核心方向,交集有限。引用 0 inbox
Shuo Ding, Qirun Zhang
crossref: 2026-09-10

AI 速览

一句话:提出基于轻量级过程间静态分析的 C++ 函数模板约束自动合成方法,可快速生成非平凡约束并缩短编译错误信息。

C++ 模板错误信息往往冗长难懂,C++20 的 constraints 与 concepts 可改善诊断,但手动指定模板约束仍然困难,在遗留项目和频繁代码变更下更甚。本文提出自动合成 C++ 函数模板约束的方法:利用轻量级静态分析检查模板体内的使用模式,以过程间方式分析,并用约束的析取建模函数重载,从而为模板的每个类型参数总结约束。作者基于 Clang 前端实现工具,并在 STL 的 algorithm 与 Boost 的 special_functions 上评估。工具可在 1.5 秒内处理超过 11 万行 C++ 代码,为 30%–40% 的函数模板合成非平凡约束;algorithm 的合成约束与标准文档吻合良好,平均可将错误信息长度分别减少 56.6% 和 63.8%。

问题
C++ 模板编译错误信息冗长且难以理解,而手动为函数模板编写 C++20 constraints 仍然非平凡,尤其在遗留项目或代码频繁变化时。
方法
采用轻量级过程间静态分析识别模板体中的使用模式,并用约束析取建模函数重载,从而自动为每个类型参数合成约束;实现基于 Clang 前端。
评估
在 STL 的 algorithm 和 Boost 的 special_functions 两个模板密集库上评估。工具在 1.5 秒内处理超过 11 万行代码,为 30%–40% 的函数模板合成非平凡约束,并使错误信息长度分别减少 56.6% 和 63.8%。
原文摘要(Abstract)

C++ templates are a powerful feature for generic programming and compile-time computations, but C++compilers often emit overly verbose template error messages. Even short error messages often involve unnecessary and confusing implementation details, which are difficult for developers to read and understand. To address this problem, C++20 introduced constraints and concepts , which impose requirements on template parameters. The new features can define clearer interfaces for templates and can improve compiler diagnostics. However, manually specifying template constraints can still be non-trivial, which becomes even more challenging when working with legacy C++ projects or with frequent code changes. This paper bridges the gap and proposes an automatic approach to synthesizing constraints for C++ function templates. We utilize a lightweight static analysis to analyze the usage patterns within the template body and summarize them into constraints for each type parameter of the template. The analysis is inter-procedural and uses disjunctions of constraints to model function overloading. We have implemented our approach based on the Clang frontend and evaluated it on two C++ libraries chosen separately from two popular library sets: algorithm from the Standard Template Library (STL) and special_functions from the Boost library, both of which extensively use templates. Our tool can process over 110k lines of C++ code in less than 1.5 seconds and synthesize non-trivial constraints for 30%–40% of the function templates. The constraints synthesized for algorithm align well with the standard documentation and on average, the synthesized constraints can reduce error message lengths by 56.6% for algorithm and 63.8% for special_functions .

链接与引用

DOI 原文 · PDF(开放获取) ·

BibTeX
@article{paperbot3184,
  title = {Fast Constraint Synthesis for C++ Function Templates},
  author = {Shuo Ding and Qirun Zhang},
  journal = {Proceedings of the ACM on Programming Languages},
  volume = {9},
  number = {OOPSLA1},
  year = {2025},
  doi = {10.1145/3720422}
}