Slides for P3776R1 — More trailing commas
- Document number:
- D3897R0
- Date:
2025-10-31 - Audience:
- EWG
- Project:
- ISO/IEC 14882 Programming Languages — C++, ISO/IEC JTC1/SC22/WG21
- Reply-to:
- Jan Schultke <janschultke@gmail.com>
- Source:
- github.com/Eisenwave/cpp-proposals/blob/master/src/more-trailing-commas-slides.cow
- →, ↓ : go to the next slide
- ←, ↑ : go to previous slide
More trailing commas
P3776R1
Introduction
- trailing commas would be useful
- [P0562R2] Trailing Commas in Base-clauses and Ctor-initializers
P0562R2
Poll: D0562R1 — forward […] to CWG for inclusion in C++26.
SF F N A SA 12 11 4 3 0
Forwarded to CWG, but currently on ice because of parsing ambiguity:
Trailing commas in other languages
Many languages now support trailing commas in parenthesized lists:
Swift
2025
2025
Kotlin
2020
2020
JavaScript
2017
2017
TypeScript
2017
2017
Rust
always
always
Go
always
always
Python
always
always
Julia
always
always
Many more (including C++) support trailing commas in braced lists.
Improved text editing
Without trailing commas, we cannot always swap or cut/paste lines:
| No trailing comma | With trailing comma |
|---|---|
|
|
|
Improved version control
| No trailing comma | With trailing comma |
|---|---|
|
|
|
- trailing comma is much better for version control:
- smaller changes when viewing line differences
- no pollution of per-line history (
git blame )
Code generation convenience for [P3294R2]
| No trailing comma | With trailing comma |
|---|---|
|
|
|
Improved auto-formatter control
- ClangFormat disables bin-packing when trailing comma is present
- this allows fine-tuned stylistic control via comma
Eliminating some uses of __VA_OPT__
| No trailing comma | With trailing comma |
|---|---|
|
|
|
is necessary to avoid error on trailing comma__VA_OPT__ - no longer necessary if trailing comma is OK
Preventing string joining bugs
| No trailing comma | With trailing comma |
|---|---|
|
|
|
- no trailing comma:
and" foe " are concatenated" fum " - missing commas happen (forgetfulness, key slip, etc.)
Where to add trailing commas
- in summary: after all lists, except:
- [P0562R2]'s
mem-initializer-list andbase-specifier-list - semicolon-terminated lists like
using a , b , ; - non-lists like
,static_assert ( true , " " , ) delete ( " " , ) - after ellipsis parameter like
void f ( int , ... , ) - in macros like
#define M(x,y,)
- [P0562R2]'s
- see proposal for examples and more details
Addressing criticisms (1/2)
- aesthetic objections
- valid, although trailing commas are 100% optional
- asymmetry with C
- just don't use the trailing comma if you need C compatibility
- WG14 proposal will be published to synchronize languages
- making
valid may invite "forgotten argument" bugsf ( 0 , ) - experience from other langs,
in C++ tells us it's okay{ 0 , } - type system, compiler warnings, auto-formatting catches this
- experience from other langs,
Addressing criticisms (2/2)
- inconsistency with macros:
may be ill-formedM ( 1 , 2 , ) - does not apply to variadic, i.e.
macros__VA_ARGS__ - macros are already inconsistent, e.g.
M ( { 1 , 2 } )
- does not apply to variadic, i.e.
- multiple ways to do the same thing
- that's okay, same for indentation and other editorial choices
- similar to
vs.// ... /* ... */
- syntax space is claimed
- no plausible alternative meaning for
exists anywayf ( 1 , )
- no plausible alternative meaning for
Last but not least,
- Implementation experience: Clang fork,
- approximately 40 LOC,
- naive implementation that ignores trailing comma,
- proper implementation may need changes to AST,
- Impact on existing code: None,
- Wording: Exists, briefly seen by CWG chair,
,
Questions
- Agreement with overall design?
- SIMD overloads in another proposal?
- Forward to LEWG?
References
[P3776R1]
More trailing commas
2025-09-09
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3776r1.html
[P0562R2]
Trailing Commas in Base-clauses and Ctor-initializers
2024-04-15
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p0562r2.pdf
[P3294R2]
Code Injection with Token Sequences
2024-10-15
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3294r2.html