Read the code. Guess the output. Get it wrong.
143 short C++ programs that compile without a warning and still do something you did not expect. Each one comes with the output it actually printed and an explanation of what the language did with your code.
Start here¶
Six that catch almost everybody. Read the snippet, commit to an answer, then open the reveal.
- 01Integer Division Truncatesthe average that loses its halfBeginner
- 20The Most Vexing Parsethe object that never existedBeginner
- 26The Container of Liesthe copy that kept in touchBeginner
- 39push_back During the Loopthe to-do list that ate itselfIntermediate
- 84The Function You Never Importedyou have been relying on this since your first *hello world*Advanced
- 90The Initialization Order Fiascotwo
.cppfiles walk into a linker…Advanced
Three collections¶
-
100 entries
The Hundred
The curated core, numbered 01 to 100 and ordered by difficulty, so an entry's number tells you what you are in for. This collection is closed.
-
21 entries
Gotchas
Traps that bit somebody in real code. No numbering, so a new one can be added without disturbing anything else. Open to contributions.
-
22 entries
Trivia
Legal C++ that has no business being legal. Each entry says how likely you are to meet it in production and cites the standard. Open to contributions.
About the output¶
Every snippet was compiled and run before it was written up. The write-ups quote the real output and the compiler's own warning text, never what it looked like the code should do. Where the standard leaves the behaviour undefined or unspecified, the entry says so and labels its output typical — your compiler is within its rights to print something else.
Each entry page also opens the snippet in Compiler Explorer with the flags already set, so you can change a line and rerun it without installing anything.
Contribute an entry¶
Found a trap that bit you at work? Gotchas and Trivia are open. An entry is a
directory holding a README.md and a main.cpp. The snippet has to look innocent, and
every claim has to come from actually running the code.