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

This document has custom controls:

  • ,  ↓ : go to the next slide
  • ,  ↑ : go to previous slide

More trailing commas
P3776R1

Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 1

Introduction

void f( int x, int y, // ← trailing comma should be allowed here ); int x[] { 1, 2, }; // already supported in braced lists
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 2

P0562R2

Poll: D0562R1 — forward […] to CWG for inclusion in C++26.

SFFNASA
1211430

Forwarded to CWG, but currently on ice because of parsing ambiguity:

struct X {}; struct Y : X, // ← OK, new trailing comma { Y() : A<b<c>(), // ← parsing ambiguity due to trailing comma { /* ... */ } /* A, b, and c declared down here somewhere */ };
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 3

Trailing commas in other languages

Many languages now support trailing commas in parenthesized lists:

Swift
2025
Kotlin
2020
JavaScript
2017
TypeScript
2017
Rust
always
Go
always
Python
always
Julia
always

Many more (including C++) support trailing commas in braced lists.

Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 4

Improved text editing

Without trailing commas, we cannot always swap or cut/paste lines:

No trailing comma With trailing comma
// before void f( int x, int y ); // after void f( int y // syntax error int x, ); // before void f( int x, int y, ); // after void f( int y, // OK int x, );
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 5

Improved version control

No trailing comma With trailing comma
void f( int x, - int y + int y, + int z ); void f( int x, int y, + int z, );
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 6

Code generation convenience for [P3294R2]

No trailing comma With trailing comma
consteval meta::info gen_params( span<const Parameter> ps ) { meta::info result = ^^{}; bool first = true; for (const Parameter& p : ps) { result = ^^{ \tokens(result) \tokens(first ? ^^{} : ^^{,}) \tokens(gen_param(p)) }; first = false; } return result; } consteval meta::info gen_params( span<const Parameter> ps, ) { meta::info result = ^^{}; for (const Parameter& p : ps) { result = ^^{ \tokens(result) \tokens(gen_param(p)) , }; } return result; }
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 7

Improved auto-formatter control

vector<int> numbers { LIST_ITEM_A, LIST_ITEM_B, // LIST_ITEM_C }; // ← column limit // vector<int> numbers { LIST_ITEM_A, LIST_ITEM_B, LIST_ITEM_C, // ← trailing comma here };
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 8

Eliminating some uses of __VA_OPT__

No trailing comma With trailing comma
#define F(...) \ f(0 __VA_OPT__(,) \ __VA_ARGS__) #define L(...) \ [& __VA_OPT__(,) \ __VA_ARGS__] {} #define F(...) \ f(0 , __VA_ARGS__) #define L(...) \ [& , __VA_ARGS__] {}
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 9

Preventing string joining bugs

No trailing comma With trailing comma
emplace_strings( "fee", "fie", "foe" + "fum" ); emplace_strings( "fee", "fie", "foe", + "fum" );
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 10

Where to add trailing commas

Discuss with clang-format maintainers.

Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 11

Addressing criticisms (1/2)

Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 12

Addressing criticisms (2/2)

Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 13

Last but not least,

Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 14
,
Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 15

Questions

Jan Schultke  |  Slides for P3776R1 — More trailing commas  |  Kona 2025  |  Slide 16

References

[P0562R2] Alan Talbot. 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] Andrei Alexandrescu, Barry Revzin, Daveed Vandevoorde. Code Injection with Token Sequences 2024-10-15 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3294r2.html