Repository navigation
Expand file tree
/
Copy pathcontrols-configured.html
More file actions
410 lines (349 loc) · 17.9 KB
/
Copy pathcontrols-configured.html
File metadata and controls
410 lines (349 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>An impldefined label next to the enforced one, and no short syntax in C++26</title>
<style>
p {text-align:justify}
li {text-align:justify}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
blockquote.quote
{
border-left: 3px solid #A0A0A0;
padding-left: 15px;
}
blockquote.std
{
background-color:#F4F4F4;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
table {border-collapse: collapse}
th, td {border: 1px solid #A0A0A0; padding: 4px 10px; vertical-align: top; text-align: left}
ins {color:#00A000}
del {color:#A00000}
</style>
</head>
<body>
<address align=right>
Document number: D4489R0<br/>
Audience: EWG, LEWG<br/>
<br/>
<br/>
<a href="mailto:ville.voutilainen@gmail.com">Ville Voutilainen</a><br/>
2026-11-20<br/>
</address>
<hr/>
<h1 align=center>An impldefined label next to the enforced one, and no short syntax in C++26</h1>
<h2>Abstract</h2>
<p>This paper builds on
<a href="https://wg21.link/p4451r2">P4451R2</a>, "Early Adoption of an
Always-Enforced Label", which adds the label
<code>std::contracts::controls::enforced</code> to C++26. It proposes a
second label, <code>std::contracts::controls::impldefined</code>, that
spells out what a C++26 contract assertion without a label does today:
the implementation chooses its evaluation semantic. It also proposes
that, in C++26, every <code>pre</code>, <code>post</code> and
<code>contract_assert</code> must name one of the two labels, in angle
brackets:</p>
<blockquote><pre><code>int f(int x)
pre<std::contracts::controls::enforced>(x > 0)
post<std::contracts::controls::impldefined>(r : r > 0)
{
contract_assert<std::contracts::controls::impldefined>(x != 7);
return x;
}
</code></pre></blockquote>
<p>The short syntax, <code>pre(x > 0)</code> with no label, becomes
ill-formed in C++26. That takes the question of what the short syntax
should mean off the table for C++26 without answering it, and leaves a
later standard free to give the short syntax whatever meaning it then
has consensus for.</p>
<p>As a lesser option, the paper also offers adding
<code>impldefined</code> without dropping the short syntax.</p>
<h2>Introduction</h2>
<p>Several people have expressed concerns about what the short syntax
does, and whether it's plausible that we're getting it right. Some want
an unlabeled assertion to be ignorable, as it is in the C++26 DIS. Others
want it to be non-ignorable, or think that the default should be decided
later with more experience. P4451R2 doesn't touch this question. It adds
an explicit spelling for one answer, <code>enforced</code>, and leaves
the short syntax meaning the other.</p>
<p>If we introduce <code>pre<impldefined></code> and drop the short
syntax, the concern about whether the default is right goes away,
because C++26 then has no default. Both choices are spelled out, side by
side:</p>
<table>
<tr><th>Always terminates on a violation</th><th>Semantic chosen by the implementation</th></tr>
<tr><td><code>pre<enforced>(x)</code></td><td><code>pre<impldefined>(x)</code></td></tr>
<tr><td><code>post<enforced>(r : r > 0)</code></td><td><code>post<impldefined>(r : r > 0)</code></td></tr>
<tr><td><code>contract_assert<enforced>(x)</code></td><td><code>contract_assert<impldefined>(x)</code></td></tr>
</table>
<p>(The table leaves out the <code>std::contracts::controls::</code>
qualification to save space.)</p>
<p>This puts the two choices on a level playing field. Neither one is
privileged by being the very terse spelling, and the committee gets more
time, and more chances to study the question, before deciding what the
short syntax should mean.</p>
<p>It also means that a reader never has to know a default to understand
what an assertion does: every assertion says which of the two behaviors
it has, and the two forms differ by a whole word. For comparison,
P4451R2 §2.1 objects to <code>pre!(x)</code> because it differs from
<code>pre(!x)</code> by one transposed character.</p>
<h2>The end result</h2>
<p>If both P4451R2 and this paper are adopted, C++26 contract assertions
look like this:</p>
<ul>
<li>The header <code><contracts></code> declares two labels in
namespace <code>std::contracts::controls</code>: <code>enforced</code>
and <code>impldefined</code>.</li>
<li>Every <code>pre</code>, <code>post</code> and
<code>contract_assert</code> has an <i>assertion-control-specifier</i>,
<code><</code> <i>id-expression</i> <code>></code>, and the
<i>id-expression</i> denotes one of the two labels.</li>
<li>An assertion labeled <code>enforced</code> is a <i>terminating
contract assertion</i>, as in P4451R2: every evaluation of it uses
<i>enforce</i> or <i>quick-enforce</i>.</li>
<li>An assertion labeled <code>impldefined</code> behaves exactly as an
unlabeled assertion does in the C++26 DIS. The implementation chooses its
evaluation semantic, and it can be any of <i>ignore</i>,
<i>observe</i>, <i>enforce</i> or <i>quick-enforce</i>.</li>
<li>An assertion without an <i>assertion-control-specifier</i> is
ill-formed.</li>
</ul>
<blockquote><pre><code>#include <contracts>
void f(int x)
pre<std::contracts::controls::enforced>(x > 0) // OK, terminating
pre<std::contracts::controls::impldefined>(x < 100) // OK, as pre(x < 100) in the DIS
pre(x != 7); // error: no label
</code></pre></blockquote>
<h2>Discussion</h2>
<h3>What <code>impldefined</code> means</h3>
<p><code>impldefined</code> adds no new behavior. Its meaning is exactly the
meaning that <a href="https://wg21.link/p2900r14">P2900R14</a> gives a
contract assertion: which evaluation semantic is used is
implementation-defined, and the implementation's options decide it. The
label only makes that choice visible in the source.</p>
<p>The name says what the standard says about the choice: it is
implementation-defined. That is the counterpart of
<code>enforced</code>, where the source makes the decision.</p>
<h3>Lookup, header and placeholder entity</h3>
<p><code>impldefined</code> follows P4451R2's rules for
<code>enforced</code> in every respect:</p>
<ul>
<li>It is found by ordinary name lookup only. A qualified name, a namespace
alias, a using-declaration and a using-directive all work. As in P4451R2,
the label-specific lookup of <a href="https://wg21.link/p3400r5">P3400R5</a>
is not proposed.</li>
<li><code><contracts></code> must be included, or the
<code>std</code> module imported, so that the declaration can be
found.</li>
<li>The <i>id-expression</i> must not be type-dependent or
value-dependent, so that the check can be made where the assertion
appears.</li>
<li>Its type is unspecified, and it is a placeholder for the label object
that P3400R5 or a successor would provide.</li>
</ul>
<p>P4451R2 §2.4 says the placeholder for <code>enforced</code> should
be in an unspecified inline namespace inside
<code>std::contracts::controls</code>, so that it can't conflict with a
future label of a different type in other translation units. P4451R2's
synopsis doesn't show that inline namespace. The same reasoning applies
to <code>impldefined</code>, so whatever the final wording does for
<code>enforced</code> should also be done for <code>impldefined</code>.</p>
<h3>Redeclarations</h3>
<p>With one possible label, P4451R2 can require that two
<i>function-contract-specifier</i>s either both have an
<i>assertion-control-specifier</i> or both don't. With two labels, and
with every specifier having one, the rule becomes that their
<i>id-expression</i>s denote the same entity. With
<code>namespace cc = std::contracts::controls;</code>,
<code>pre<cc::enforced>(x)</code>
and <code>pre<std::contracts::controls::enforced>(x)</code> are the
same; <code>pre<cc::enforced>(x)</code> and
<code>pre<cc::impldefined>(x)</code> are not.</p>
<p>Under the lesser option described below, a specifier can also have no
label. An unlabeled specifier and one labeled <code>impldefined</code> have
the same meaning, but whether they count as the same on a redeclaration
is a design choice. The simplest rule is that they don't: both
declarations have to be written the same way.</p>
<h3>Forward compatibility</h3>
<p>Making the label mandatory in C++26 keeps every option open for later
standards:</p>
<ul>
<li><b>Re-allowing the short syntax is a pure extension.</b> A later
standard can make <code>pre(x)</code> well-formed again and give it the
meaning that has consensus then. That could be the meaning of
<code>impldefined</code>, the meaning of <code>enforced</code>, or
something else. Because no C++26 program contains the short syntax, none
of them changes meaning. The opposite isn't true: once C++26 ships the
short syntax with a meaning, changing that meaning breaks code
silently.</li>
<li><b>P3400R5.</b> As in P4451R2, the name between the angle brackets
denotes the entity that would become the label object under P3400R5.
P3400R5 would need a label with <code>impldefined</code>'s meaning: one
whose set of allowed semantics is all four semantics, and which
doesn't otherwise alter the semantic that the configuration chooses.
Since it restricts nothing, combining it with other labels shouldn't
change what they do. Its name in P3400 is for LEWG to decide, but
choosing <code>impldefined</code> would keep C++26 code valid without
change.</li>
<li><b>Other directions.</b> If P3400R5 isn't pursued, P4451R2 §2.6
notes that only the semantics of <code>enforced</code> is binding. The
same holds for <code>impldefined</code>: its semantics is the semantics
C++26 contract assertions have anyway.</li>
</ul>
<h3>Every contract assertion needs <code><contracts></code> or <code>import std</code></h3>
<p>A label is found by name lookup, so its declaration has to be visible
where the assertion is written. With the short syntax, a contract
assertion needs no header at all. P4451R2 requires
<code><contracts></code>, or <code>import std</code>, only for an
assertion labeled <code>enforced</code>. Dropping the short syntax
extends that requirement to every contract assertion:</p>
<blockquote><pre><code>// No #include <contracts> and no import std.
int f(int x)
pre(x > 0); // DIS: OK
// this paper: error, no label
int g(int x)
pre<std::contracts::controls::impldefined>(x > 0); // error: name not found
</code></pre></blockquote>
<p>In practice, every header that declares a function with a precondition
or a postcondition includes <code><contracts></code> or imports
<code>std</code>, and so does every source file that uses
<code>contract_assert</code>. A header that uses contracts can't avoid
exposing <code><contracts></code> to its users.</p>
<p>Implementations can also reduce the cost outside the standard. The p4324
branch of GCC has an option, <code>-fcontracts-implicit-include</code>,
that includes <code><contracts></code> implicitly at the start of
every translation unit. Code that relies on such an option isn't
portable, so it is a convenience for a project's own builds, not a
replacement for the <code>#include</code> or <code>import std</code>.</p>
<p>The requirement isn't permanent. If a later standard brings the short
syntax back, assertions written with it need no header again.
The lesser option described below doesn't have this cost:
the short syntax stays, and needs no header.</p>
<h3>Costs</h3>
<ul>
<li><b>Every contract assertion needs <code><contracts></code> or
<code>import std</code>.</b> See the previous section.</li>
<li><b>Existing code using the short syntax must be changed.</b> The only
such code is code written against implementations of the C++26 drafts.
The change is mechanical: add <code><impldefined></code>, which
keeps the behavior exactly. Implementations can keep accepting the short
syntax as an extension, with the diagnostic an ill-formed program
requires. A fix-it can insert the label.</li>
<li><b>Verbosity.</b> Every assertion now names a label. As P4451R2
shows, a namespace alias or a using-directive shortens the name. The
label-specific lookup of P3400R5 would shorten it, and can be considered
for a later standard along with the short syntax.</li>
</ul>
<p>The library clauses specify preconditions in prose
(<i>Preconditions:</i>, <i>Hardened preconditions:</i>), not with the
contract-assertion syntax, so they need no change. Examples in the core
clauses that use the short syntax would need a label.</p>
<h2>Proposal</h2>
<blockquote class="note">
<p>On top of P4451R2:</p>
<ol>
<li>Add <code>std::contracts::controls::impldefined</code> to
<code><contracts></code>, a label whose assertions use the
implementation-defined evaluation semantic that an unlabeled assertion
uses in the C++26 DIS.</li>
<li>Make the <i>assertion-control-specifier</i> mandatory on
<code>pre</code>, <code>post</code> and <code>contract_assert</code>,
and require its <i>id-expression</i> to denote either
<code>std::contracts::controls::enforced</code> or
<code>std::contracts::controls::impldefined</code>.</li>
</ol>
</blockquote>
<h2>A lesser option</h2>
<p>If the committee isn't willing to drop the short syntax from C++26, a
lesser option is to adopt only the first half of the proposal: add
<code>std::contracts::controls::impldefined</code> and keep the short
syntax with its DIS meaning. Then C++26 has three spellings:</p>
<blockquote><pre><code>#include <contracts>
namespace cc = std::contracts::controls;
void f(int x)
pre<cc::enforced>(x > 0) // terminating
pre<cc::impldefined>(x < 100) // semantic chosen by the implementation
pre(x != 7); // the same as pre<cc::impldefined>(x != 7)
</code></pre></blockquote>
<p>What the lesser option gives:</p>
<ul>
<li>Authors who want every assertion to state its behavior can write it
that way, and the two explicit forms differ by a whole word.</li>
<li>Coding guidelines, and compiler warnings outside the standard, can
require a label on every assertion. Code that follows them is already
written the way the main proposal would require.</li>
<li>Code written with explicit labels keeps its meaning whatever a later
standard decides about the short syntax.</li>
<li>The label is a placeholder for a P3400 label in the same way that
<code>enforced</code> is, with the same forward compatibility.</li>
<li>Code that uses only the short syntax still needs neither
<code>#include <contracts></code> nor <code>import std</code>.</li>
</ul>
<p>What it doesn't give: the short syntax still has a meaning in C++26, so
C++26 still answers the question of what the default should be. The
concern about whether that answer is right doesn't go away, and changing
the meaning of the short syntax later would silently change the meaning
of existing code. That is why this paper prefers the full proposal.</p>
<blockquote class="note">
<p>Lesser option, on top of P4451R2: add
<code>std::contracts::controls::impldefined</code> to
<code><contracts></code>, with the meaning described above. The
short syntax stays valid and means the same as
<code><impldefined></code>.</p>
</blockquote>
<h2>Implementation experience</h2>
<p>The p4324 branch of GCC, which implements
<a href="https://wg21.link/p4324">P4324</a>, implements the same
functionality with a different spelling. It is available on Compiler
Explorer. Its <code><contracts></code> provides <i>control
objects</i>, P4324's counterpart of labels, which are named between the
angle brackets in the same position:</p>
<table>
<tr><th>This paper</th><th>p4324 branch</th></tr>
<tr><td><code>pre<std::contracts::controls::impldefined>(x)</code></td>
<td><code>pre<std::contracts::default_v>(x)</code></td></tr>
<tr><td><code>pre<std::contracts::controls::enforced>(x)</code></td>
<td><code>pre<std::contracts::mandatory_v>(x)</code></td></tr>
</table>
<p><code>default_v</code> uses the evaluation semantic the implementation
is configured with, as an unlabeled assertion does.
<code>mandatory_v</code> is checked under every configuration and
terminates on a violation. The same spellings work for
<code>post</code> and <code>contract_assert</code>, and are found by
ordinary name lookup.</p>
<h2>Conclusion</h2>
<p>P4451R2 gives C++26 an explicit spelling for one answer to "what does a
contract assertion do on a violation". This paper adds the explicit
spelling for the other answer and removes the implicit one. C++26 then
doesn't decide what the short syntax means; it doesn't have one. The
short syntax can come back in a later standard, as a pure extension,
once the committee has had the time and the study to decide what it
should mean.</p>
<h2>References</h2>
<ul>
<li><a href="https://wg21.link/p4451r2">P4451R2</a>: Joshua Berne,
"Early Adoption of an Always-Enforced Label". 2026-11-19</li>
<li><a href="https://wg21.link/p2900r14">P2900R14</a>: Joshua Berne, Timur
Doumler, Andrzej Krzemieński, "Contracts for C++". 2025</li>
<li><a href="https://wg21.link/p3400r5">P3400R5</a>: Joshua Berne,
"Controlling Contract-Assertion Properties". 2026</li>
<li><a href="https://wg21.link/p4324">P4324R0</a>: Ville Voutilainen,
"Exploration of a more library-oriented approach to contracts".
2026-08-14</li>
<li>Implementation: <a href="https://github.com/villevoutilainen/gcc/tree/p4324">https://github.com/villevoutilainen/gcc/tree/p4324</a></li>
</ul>
</body>
</html>