For the first time, a C++ standard failed to pass the national bodies

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Bjarne Stroustrup, in the flesh. Toggle original/dithered image Pops cork. It looks like it happened! A historic first, for better or worse. This is a sign that C++ is (still) struggling with its governance (see a previous article on cultural issues within C++ here). This is a big deal even if you do not care about programming languages.

Bjarne Stroustrup 本人。切换原始/抖动图像 开香槟。看来事情真的发生了!无论好坏,这都是史无前例的第一次。这表明 C++(依然)在治理方面举步维艰(参阅此处关于 C++ 内部文化问题的往期文章)。即使你不关心编程语言,这也是一件大事。

C++ is arguably the most important programming language in existence. Your computer runs on C or C++. The web browser you are using to read this is written in C++. ~All programming languages depend on C++ (often through LLVM). Likewise, ~all websites with performance requirements (e.g. YouTube) depend on C++, ~all data storage, game engines, finance, etc. In a world that is drowning in exciting new vulnerabilities in ~all software, the ability of C++ to change and adjust to changing times affects you.

C++ 可以说是现存最重要的编程语言。你的电脑运行在 C 或 C++ 之上。你用来阅读本文的网页浏览器就是用 C++ 编写的。几乎所有的编程语言都依赖于 C++(通常通过 LLVM)。同样,所有有性能要求的网站(例如 YouTube)都依赖 C++,还有所有的数据存储、游戏引擎、金融系统等等。在一个充斥着各种软件漏洞的世界里,C++ 适应时代变化的能力与你息息相关。

Let’s take a step back and get an overview. The initial source was a post by Robert Seacord on LinkedIn: “Balloting has completed for C++26. Unfortunately it was Disapproved. P-Members voting: 12 in favor out of 20 = 60 % (requirement >= 66.66%) (P-Members having abstained are not counted in this vote.) Member bodies voting: 9 negative votes out of 22 = 41 % (requirement <= 25%). Sort of a sad day for the C++ community. Hopefully we can heal and move on from this in a productive manner.”

让我们退一步,先了解一下概况。最初的消息来源是 Robert Seacord 在 LinkedIn 上发布的一篇文章:“C++26 的投票已经结束。遗憾的是,它未获通过。P 成员投票情况:20 票中有 12 票赞成,占比 60%(要求 >= 66.66%)(弃权的 P 成员不计入本次投票)。成员机构投票情况:22 票中有 9 票反对,占比 41%(要求 <= 25%)。对于 C++ 社区来说,这真是令人难过的一天。希望我们能从中恢复,并以富有成效的方式继续前进。”

In a comment, J. Daniel García adds: “There were 12 countries that abstained. In several cases because they could not reach to a common position. That is another signal of a contested draft.” Now, before we get into the facts: This is not a good look for C++. There have been multiple fights over the future direction of the C++ language, but no C++ standard ever failed to be signed off by the national bodies.

J. Daniel García 在评论中补充道:“有 12 个国家弃权。在某些情况下,是因为他们无法达成共识。这是草案存在争议的另一个信号。” 现在,在进入事实细节之前:这对 C++ 来说不是什么好兆头。关于 C++ 语言的未来方向,此前已经发生过多次争论,但从未有任何 C++ 标准未能获得国家机构的签署通过。

The national body vote is considered to be a formality. By the time the vote reaches the national bodies all disagreements should’ve been sorted out by WG21 (Working Group 21, i.e. the C++ Standards Committee). Truly controversial features should never make it this far, since they should never be added to the C++26 working draft in the first place. This is a sign of division in the C++ community, in some form or another, and completely independently of whether you agree with this result or not.

国家机构投票通常被视为一种形式。当投票到达国家机构时,所有分歧本应由 WG21(第 21 工作组,即 C++ 标准委员会)解决。真正有争议的功能本不应走到这一步,因为它们根本就不应该被加入到 C++26 的工作草案中。无论你是否同意这一结果,这都标志着 C++ 社区在某种程度上存在分歧。

Here, “Added to the C++26 working draft” is essentially accepting the pull request. Everyone is working towards the C++26 release, and people contribute to it with papers. Papers lead to votes, which then lead to amendments of the draft. Finally (this is where we are) wording passes happen, and it gets signed off, rubber-stamped and released. Phrased like this, the ISO process sounds sensible enough, but trust me, it’s actually kind of a nightmare.

在这里,“加入 C++26 工作草案”本质上就是接受了合并请求(pull request)。每个人都在为 C++26 的发布而努力,人们通过提交论文来做出贡献。论文引发投票,进而导致草案的修订。最后(也就是我们现在所处的阶段)进行措辞审查,然后签署、盖章并发布。这样表述的话,ISO 的流程听起来还算合理,但相信我,它实际上是一场噩梦。

Needless to say, the debates are raging, with comments on all sides: So many discussions. Wait, how did the voting break down? What are P-members??? Quick explanation: The national bodies execute a single vote. To pass the national bodies, the draft has to meet two bars: At least two-thirds of the participating voting members have to vote Yes. At most one quarter of all voting members may vote No. If the above posts are correct, then the draft failed to meet both bars. Twelve isn’t two-thirds of twenty, and nine is more than a quarter.

不用说,辩论正在激烈进行,各方评论不断:讨论太多了。等等,投票结果是怎么分布的?什么是 P 成员???简单解释一下:国家机构执行单一投票。要通过国家机构的投票,草案必须达到两个门槛:至少三分之二的参与投票成员必须投赞成票;最多不超过四分之一的投票成员投反对票。如果上述帖子属实,那么该草案未能达到这两个门槛。12 不是 20 的三分之二,而 9 超过了四分之一。

Visualization: In total, there were 20 P-members, and 22 total members (ignoring abstaining). I heard that the US abstained, but take that with a grain of salt. So, what happened? tl;dr: WG21 voted to include Contracts in C++26. Several high-ranking members of WG21 are strongly opposed to Contracts, and so are several national bodies. The national body vote was the ’last resort’ place to prevent Contracts from being canonicalized as part of the C++ standard.

可视化:总共有 20 个 P 成员,总计 22 个成员(忽略弃权者)。我听说美国弃权了,但对此持保留态度。那么,到底发生了什么?简而言之:WG21 投票决定将“契约”(Contracts)纳入 C++26。WG21 的几位高级成员强烈反对契约,几个国家机构也是如此。国家机构投票是防止契约被正式纳入 C++ 标准的“最后一道防线”。

Wait, that’s too fast for me, what are Contracts? New keywords that replace assert macros with a standardized way to enforce runtime checks. Most importantly, you can add pre and post checks to ensure that all arguments and return values fulfill certain conditions. This ends up looking roughly as follows: int wrap_index(int i, const int size) pre (size > 0) // the caller must guarantee this post (r: 0 <= r && r < size) // the function promises this; r is the return value { int r = i % size; if (r < 0) r += size; contract_assert(r >= 0); // a check inside the body return r; }

等等,这太快了,什么是契约?这是新的关键字,用标准化的方式取代 assert 宏来强制执行运行时检查。最重要的是,你可以添加前置和后置检查,以确保所有参数和返回值满足特定条件。最终代码看起来大致如下:int wrap_index(int i, const int size) pre (size > 0) // 调用者必须保证这一点 post (r: 0 <= r && r < size) // 函数承诺这一点;r 是返回值 { int r = i % size; if (r < 0) r += size; contract_assert(r >= 0); // 函数体内的检查 return r; }

Looks simple, right? You’d think so, but no! First, due to issues with virtual functions, Contracts for virtual functions were already cut from the proposal. Second, it’s not entirely obvious how this will interact with constexpr or function inlining. Third, there are compile flags to selectively enable or disable the enforcement. E.g. “If a contract is violated, do you want to 1) kill the program, 2) call a ‘contract-violation handler’ and then kill the program, 3) log an error, 4) ignore completely (and not even perform the check, for performance reasons)?”

看起来很简单,对吧?你可能会这么想,但事实并非如此!首先,由于虚函数的问题,虚函数的契约已经从提案中删除了。其次,它将如何与 constexpr 或函数内联交互并不完全明确。第三,存在用于选择性启用或禁用强制执行的编译标志。例如:“如果违反了契约,你想要 1) 终止程序,2) 调用‘契约违规处理程序’然后终止程序,3) 记录错误,4) 完全忽略(出于性能原因,甚至不执行检查)?”

Big question: You compile your code with enforce and are linking a prebuilt library that pulls in the same dependency as you, but with Contracts set to ignore. This is fine according to Contracts. The linker gets to keep one of the copies. What happens? Do Contracts run or not? What if a call is inlined across translation units? Is there even a way for the user to guarantee that checks always run? (The answer is no. What? You were hoping that this feature could be used to ensure certain checks guarding against Undefined Behavior would always run, no matter what? Don’t be silly—this is C++.)

大问题:你用强制执行(enforce)编译代码,并链接了一个预构建的库,该库引入了与你相同的依赖项,但契约设置为忽略(ignore)。根据契约规则,这是允许的。链接器会保留其中一个副本。会发生什么?契约会运行吗?如果调用跨翻译单元内联了怎么办?用户甚至有办法保证检查总是运行吗?(答案是不能。什么?你希望这个功能可以用来确保某些防止未定义行为的检查无论如何都会运行?别傻了——这是 C++。)

Oh, did I mention that code inside of predicates is just standard C++, so the compiler is allowed to elide checks due to Undefined Behavior? But do feel free to take it from the source: The “Contracts paper” can be found here, and if you read closely, you’ll find some funny statements that really ruffled some feathers: It is ill-formed, no diagnostic required (IFNDR) if multiple first declarations for the same function are in different translation units that do not have the same sequence of function contra

哦,我提到过谓词内部的代码只是标准的 C++ 吗?因此,编译器可以因为未定义行为而省略检查。但请随意查阅原始资料:“契约论文”可以在这里找到,如果你仔细阅读,会发现一些真正让人恼火的有趣声明:如果同一函数的多个首次声明位于不同的翻译单元中,且这些声明没有相同的函数契约序列,则该程序格式错误,且无需诊断(IFNDR)。