将C字符串字面量设为const。
Make C string literals const?

原始链接: https://gustedt.wordpress.com/2025/04/06/make-c-string-literals-const/

Martin Uecker正在探讨C2y中一项更改,即默认情况下将字符串字面量设为const限定,使C与C++保持一致。虽然编译器多年来一直通过选项(例如`-Wwrite-strings`)支持此功能,但这仍然是一个可能带来破坏性影响的规范性更改。 在继续之前,Uecker希望获得关于此更改对现有C代码库的影响的具体反馈。他请求开发人员提供信息,重点关注事实经验,而不是意见。他对以下项目特别感兴趣: * 已经使用选项将字符串字面量视为const的项目。 * 已经使用此类选项进行测试,并且可以轻松采用此更改的项目。 * 已经过测试,但会发现永久采用此更改存在问题的项目。 * 忽略const正确性,并且可能会禁用此功能的项目。 Uecker强调需要提供真实的案例,并请求提供开源项目的指针或商业项目的描述(避免泄露机密信息),以及关于const限定选项使用情况的报告。

Hacker News 最新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 将 C 字符串字面量设为 const? (gustedt.wordpress.com) ingve 2小时前 7 分 | 隐藏 | 过去 | 收藏 | 讨论 加入我们,参加 6 月 16-17 日在旧金山举办的 AI 初创公司学校! 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:
相关文章

原文

Martin Uecker has started a new initiative to ensure a better const contract for C2y: change the type of string literals to a const-qualified base type, much as it is already the case in C++. Compilers support this since a very long time; some of them have this as default, some provide command line switches for that model.

Nevertheless, this would be normative change and might be some burden for existing code. So, before doing this and writing papers, it would be good if we had an idea of the impact of such a change in existing code bases. I would be very grateful if we’d receive feedback from you along the lines of

  • You have a project that already uses options such as gcc’s -Wwrite-strings (or even a compiler with such a default) to have all string literals const-qualified.
  • You have a project and you tested it with such options and introducing this change would be easy. (If so does this change expose some qualification bugs in your code base?)
  • You have a project and you tested it with such options, but introducing this permanently into your code base would be a real pain.
  • Your project does not care about const and you wouldn’t even know where to begin. If it were introduced in a future version of C, you probably you would have to use the command line to switch such a feature off.

The goal here is not to have an opinion poll or similar; I am not convinced that such polls on some random blog like mine have any particular meaning. I really like to have facts first, not opinions:

  • If it is open source, please give a pointer to your project. Otherwise, please describe your project e.g if it is a commercial product of your company or employer. But please, do not share confident information that could get you in trouble.
  • Report on your experience with these kind of options for const qualification.
  • Don’t speculate about what could happen, restrict yourself to facts.
联系我们 contact @ memedata.com