LIVE SUBMISSIONS — 3 DAYS

C++26 Goes Back to Ballot With pre<enforced>. Inlined, GCC 16's Contract Still Loses to an if

Published: November 23, 2026 | C++ Language & Standards Compilers & Toolchains


On Saturday 21 November, in Búzios, WG21 voted 111 to 9 to send C++26 to a second ISO ballot. Contracts, the feature that sank the first ballot in October, stay in. The committee made one change to them before voting: a way to write, in the source, a check that no build flag can switch off. It is spelled like this:

#include <contracts>

int f(int x)
    pre<std::contracts::controls::enforced>(x > 0);

The name between the angle brackets was chosen on 19 November. A precondition spelled this way is checked in every build and a violation never returns into the function, whatever the build flags say about the other contracts in the program.

The first ballot failed four weeks ago, twelve national bodies in favour where fourteen were needed, and contracts were the reason. In that piece I showed that GCC 16.2 compiles an enforced contract to six instructions where the hand-written if (x < 0) std::terminate(); compiles to four. I counted instructions and timed nothing.

So there are two questions for anyone who ships C++. What does the new label compile to, and what do the two extra instructions cost in cycles? On the two compiler branches that implement the label it compiles to the same four instructions as the if, and it survives a build with every other contract switched off. The four instructions are those branches' doing and an unlabeled contract gets them too, while GCC 16.2 and GCC trunk of 21 November reject the label as a syntax error and still emit six. Timed on aorus, the six-instruction form costs under 1% behind a function call, and between 25% and a factor of three once the checked function is inlined into a loop.

What Búzios decided

Herb Sutter's trip report went up the day the meeting closed. Most of the six days went to a second set of national-body comments, "primarily on the contracts language feature". Several of those comments asked for contracts to come out of C++26, and the committee "discussed, revoted, and reaffirmed it wants to retain contracts in C++26". Then it voted to reship. Sutter gives the three plenary votes that matter, each as in favour, opposed, abstaining:

Plenary Question Vote In favour of those voting
February 2025 adopt contracts into the working draft 100 / 14 / 12 87.7%
March 2026, Croydon send C++26 to its first ballot 114 / 12 / 3 90.5%
November 2026, Búzios reship C++26, contracts kept, one extension added 111 / 9 / 8 92.5%

Two weeks before the meeting Sutter had laid out six ways forward in P4454R0, a paper whose first sentence says the ballot failed because "the language's creator strongly disagreed with the consensus of its steward body". Option B was to remove contracts and ballot again, and an editor's note in the paper records that Bjarne Stroustrup "stated here, when reviewing this paper, that Option B is the outcome he most wants to see". The committee took option D instead, which ballots the post-Búzios draft with the one addition several national bodies had asked for since the committee-draft stage. Whether that moves the five bodies that raised contracts concerns for the first time with a Disapprove vote in October is a question about people, and I have no measurement for it.

Three other papers went into C++26 the same week: P4466R2 on reflection of using-declarators with modules, P3861R1 letting string_view, span and tuple be template arguments, and P4453R1, which Sutter describes as offering "more flexible control over memory allocation with std::execution". That last one deserves a read by anyone who has kept senders out of a hot path over allocation, and I have not read it yet. Sutter gives no date for the second ballot. Under the ISO Directives a revised draft gets an eight-week vote.

What the label says

The adopted paper is P4451R2, "Early Adoption of an Always-Enforced Label", by Joshua Berne of Bloomberg. The wording change is one clause: "the evaluation semantic used for the evaluation of a terminating contract assertion is a terminating semantic." In P2900's vocabulary that means enforce or quick_enforce, never ignore or observe. The label works on pre, post and contract_assert, it needs <contracts> or import std, and it has to be found by ordinary name lookup, so the long name can be shortened the usual way:

namespace scc = std::contracts::controls;

bool push(int v)
    pre<scc::enforced>(tail_ - head_ < CAPACITY);   // on in every build

The recommended practice changed with it. An implementation is now asked for an option under which "all evaluations of contract assertions that are not terminating contract assertions use the ignore semantic". So the everything-off build keeps the labeled checks, by design.

This is a subset of P3400R5, the general labels proposal aimed at C++29, and it is the committee's answer to Romania's P3911R2, whose first revision spelled the same guarantee pre!. According to a footnote in P4454R0, EWG polled that direction in a January telecon and it failed 8 to 25 with 9 neutral. Ten months and one failed ballot later it is in the draft, and the paper that put it there is frank about how it feels: "If the committee is going to decide to do what we have actual consensus for, this paper should not be adopted". Its conclusion calls the adoption "rushing a small part of what [P3400R5] provides into C++26 for questionable reasons" and then says that doing so "would certainly be a better option than holding up the entire Contracts facility." That revision is dated 19 November. Plenary adopted it two days later.

The hazard it answers, on GCC 16.2

Sutter's footnote states the request in one line: a contract "guaranteed to be enforced at execution time regardless of build mode or linking". The second half of that is easy to show on a compiler you can install. One header, one inline function with an ordinary precondition:

// half.hpp
inline int half(int x) pre(x >= 0) { return x / 2; }

// a.cpp, built with -fcontract-evaluation-semantic=ignore
int call_a(int x) { return half(x); }

// b.cpp, built with -fcontract-evaluation-semantic=enforce
int call_b(int x) { return half(x); }

On aorus with GCC 16.2.0 (built from the releases/gcc-16.2.0 tag, -std=c++26 -fcontracts), I called each with -1 read from the command line and linked the two objects in both orders:

Build Link order call_a(-1), file built with ignore call_b(-1), file built with enforce
-O0 a.o b.o returns 0 returns 0
-O0 b.o a.o terminates terminates
-O2, half not inlined a.o b.o returns 0 returns 0
-O2, half not inlined b.o a.o terminates terminates
-O2, half inlined either returns 0 terminates

Each object carries its own weak copy of half(int), the linker keeps the first one it meets, and the check exists or does not exist for the whole program depending on the order of two file names on a link line. When it fires the message is contract violation in function int half(int) at half.hpp:7: x >= 0, also for the caller that asked for ignore. At -O2 with inlining each call site gets its own file's semantic, which is consistent and still not what the author of half wrote down. Nothing here is a GCC bug. P2900 allows the semantic to differ per translation unit and this is what that permission looks like after the linker has run.

The label takes the build out of it by putting the check into every copy. I verified the per-file half of that (next section). I did not link a mixed program on the label branches, because Compiler Explorer compiles one file at a time and I have not built either branch locally.

What the label compiles to

P4451R2 says the feature is implemented "in branches of GCC, Clang, and the EDG C++ front end, available on Compiler Explorer, behind the -fcontracts-p4451 flag". The GCC one is listed as "x86-64 gcc (P3850 contracts)" and reports itself as GCC 17.0.0 20261110. I compiled the three kernels from the October piece there with -std=c++26 -fcontracts -fcontracts-p4451 -O2 -march=znver2, the same flags as before plus the feature switch. This is abs_pre with the label:

# int abs_pre(int x) pre<std::contracts::controls::enforced>(x >= 0)
abs_pre:
        testl   %edi, %edi
        js      .L7
        movl    %edi, %eax          # no neg, no cmov: x >= 0 is known here
        ret
.L7:
        subq    $8, %rsp
        movl    $.Lcontract_data0, %edi
        call    __cxa_contract_violation_pre_enforce_pf   # nothing after it

That is four instructions on the path where the check passes, the count of the hand-written if. The rest of the census, all on abs_pre:

Compiler Contract Semantic flag Check emitted Instructions abs kept
P3850 branch pre<enforced> default yes 4 no
P3850 branch pre<enforced> ignore yes 4 no
P3850 branch pre<enforced> observe yes, as enforce 4 no
P3850 branch pre<enforced> quick_enforce yes, ud2 4 no
P3850 branch pre enforce yes 4 no
P3850 branch pre ignore no 4 yes
P3850 branch none, if (x < 0) std::terminate(); yes 4 no
GCC 16.2 pre enforce yes 6 yes
GCC trunk, 17.0.0 20261121 pre enforce yes 6 yes
GCC 16.2 and trunk pre<enforced> does not compile

Rows two and three are the label doing its job: with -fcontract-evaluation-semantic=ignore the labeled function keeps its check while an unlabeled one in the same file compiles to the bare mov; neg; cmovs; ret. The Clang branch agrees on the rows I ran there: the label, the unlabeled contract, the if, and the label under ignore.

Row five is the one to read twice. On the branch an unlabeled contract under enforce produces the same bytes as the labeled one, so the label is not what brought the count from six to four. The branch lowers a violation to one call that the optimizer treats as not returning, and it rejects -fcontracts-conservative-ipa, the GCC 16 default behind October's six instructions, as an unrecognized option. Stock GCC still has it: trunk built on 21 November routes the violation through __tu_terminate_wrapper as 16.2 does. I don't know whether the branch's lowering is headed upstream, and I have not asked on the GCC list. I had also predicted that quick_enforce would be a direct call to std::terminate, since that is what GCC 16.2 does behind its wrapper. On the branch it is a two-byte ud2.

What the caller may assume

The section of P4451R2 that will get quoted least is 2.3. An always-enforced precondition looks like a fact the caller could use:

void f(int x) pre<std::contracts::controls::enforced>(x > 0);  // defined elsewhere

void g(int x) {
    f(x);                  // terminates if x <= 0
    if (x > 0) {           // so this test is redundant. Is it?
        for (int i = 0; i != x; ++i) send_money_to_friends();
    }
}

The example is adapted from the paper, and the paper's answer is no. If f is later relinked from a library whose f dropped the precondition, a caller that removed its own test runs the loop with a negative x. Its recommendation reads "For the C++26 timeframe, we recommend doing nothing and accepting the likely solution in the first bullet above", and the first bullet is that implementations "wrap such remote contract assertions in barriers that prevent optimizations", with the admitted "downside of preventing many optimizations that would be perfectly valid".

On the branch g behaves as the paper wants. With the default callee-side checking it calls f and then tests x again (call f(int), testl %ebp, %ebp, jle), also when f is defined in the same file and merely not inlined. The caller gets the fact only when it performs the check itself. With -fcontracts-client-check=pre the test of x moves in front of the call, the second one disappears and the loop runs unguarded, which is sound because g just looked. I had predicted the opposite, expecting the caller-side check to sit in a separate wrapper function. The price is in the paper and I confirmed it with a predicate that prints: one line of output per call with callee-side checks, two with caller-side checks on, because the callee still has to check for the callers that did not.

So across a function boundary the label buys the guarantee and no speed. The implementation declines to trust a promise that a relink could break.

What six instructions cost

October's census left the obvious question open. I timed it on aorus (Threadripper 3960X, Zen 2, core 18 isolated with isolcpus and nohz_full, boost off) on 21 November with GCC 16.2.0 at -O2 -march=znver2. Four builds of the same kernels were linked into one binary: no check, the hand-written if (!c) std::terminate();, the contract under enforce as GCC ships it, and the contract with -fno-contracts-conservative-ipa. Each trial runs 65,536 calls per build with the builds interleaved and their order rotated, 1,001 trials after 50 warm-up trials, counting user-mode core cycles and instructions with perf_event_open. Every build has to return the same value from every loop before anything is timed. The numbers are median cycles per call; the IQR is at most 0.003 cycles in the first row and under 0.001 in the others.

Loop No check Hand-written if Contract, GCC 16.2 default Contract, conservative IPA off
ring push, behind a call 8.002 8.578 8.634 8.578
abs, behind a call, result fed to the next call 5.002 5.002 5.002 5.002
abs, behind a call, independent arguments 6.002 6.002 6.002 6.002
ring push, inlined into a loop of 1,024 1.902 2.022 2.522 2.022
abs, inlined, result fed to the next iteration 3.001 1.002 3.128 1.002

Behind a call the two instructions are invisible. I expected the chained abs to show about two cycles, since the surviving neg and cmov sit on the dependency chain, and it shows nothing: all four builds land on the same whole number of cycles, including the two whose callee has no neg at all. The loop is bound by the call. In an earlier build of the same harness that number was 6.002 for the chained loop and 5.002 for the independent one, the reverse of the table, still identical across the four builds. I don't know what sets that floor. My guess is the placement of the loop and its callees relative to each other, and I did not chase it, because a one-cycle step that moves with the link is larger than anything the contract does here. For push the contract costs 0.056 cycles per call over the if, 0.65%. That is nineteen times the IQR and it came out the same in three runs, so it is real, and it is small.

Inlined, it stops being small. In the ring loop the hand-written check lets GCC read head_ once before the inner loop; the contract build reloads it on every iteration and saves five registers on entry, 14 instructions per iteration against 11, and runs 25% slower. I had predicted at least 50%. The chained abs is the clean case:

# hand-written if, 1.002 cycles          # contract as shipped, 3.128 cycles
.L4: test  %eax,%eax                     .L4: test   %eax,%eax
     js    .cold                              js     .cold
     inc   %rdx                               mov    %eax,%ecx
     add   %esi,%eax                          neg    %ecx
     cmp   %rdx,%rdi                          cmovns %ecx,%eax
     jne   .L4                                inc    %rdx
                                              add    %esi,%eax
                                              cmp    %rdx,%r8
                                              jne    .L4

The left loop has one add on its chain and the right has three operations, and the cycle counts say the same. The build with no check at all is 3.001, so on GCC 16.2 the hand-written check is three times faster than writing nothing. The check is what tells the optimizer x >= 0, and the shipped contract checks without telling. With the conservative lowering switched off the contract matches the if instruction for instruction in all five loops. My first version of this loop masked the accumulator to 16 bits, GCC proved the precondition from the mask, and all four builds compiled to the same five instructions. That run measured nothing and I threw it out.

On the label branch the two inlined loops compile to the same number of instructions per iteration as the hand-written check, 6 and 11, in a different block order. I could not time them, having no build of the branch on aorus, so the 1.0 and 2.0 in the table are what I expect for the label there and not something I measured. Of sixteen predictions written down before the runs, eleven held, four missed and one was half right.

What changes for code you compile today

In a compiler you can install, nothing changed on 21 November. pre<std::contracts::controls::enforced> is a syntax error in GCC 16.2 and in GCC trunk of that date (expected '(' before '<' token), and __cpp_contracts will move from 202502L to 202611L in an implementation that has it. The mixed-build hazard in the table above is in every GCC 16 installation now, for every inline function with a contract that is compiled under more than one setting.

For the hot path the numbers from October hold and now have cycles attached. On GCC 16.2 the hand-written check is the fastest of the spellings that check anything, by a factor of 3.1 in a dependent loop and 1.25 in the ring. -fno-contracts-conservative-ipa closes the gap completely and reopens the hazard the flag exists for. The label, when it reaches a release, will give you the guarantee in source, and on the evidence of the two branches the same code as the if. That second part depends on GCC giving up the conservative lowering upstream, which is a separate decision from adopting P4451.

P4454R0 ends with a request to both camps on contracts: "agree in advance on what measurable evidence would change minds", and then "Please be measurable." The second ballot will be decided by national bodies reading papers. For what a contract costs a trading loop on the compiler that ships today, the count is 3.128 cycles against 1.002.

Review quiz

Pass it to mark this article as read and count it toward your reading achievements.

Five questions about this article. Get four right and it counts as read. Review quizzes come with a membership.

Log in See membership

Back to Courses
116 reads 0

Comments

No comments yet. Be the first to share your thoughts.

Log in as a member to comment, or subscribe to join the discussion.