The Missing
C++17 Reference

Learning to program, in C++17, from nothing.

Start with Chapter 0 →

Contents

Index of terms

Part 0: Getting a compiler to obey

  1. 0Installing a Compiler Without Losing Your MindBefore you can write a line of C++, your computer needs a compiler. This is the hour that stops most people. Read the section for your own machine, ignore the other two, and finish with a program you built yourself.Project: Your toolchain

Part 1: Writing programs

Productive from the first chapter, using the tools C++ actually gives you.

  1. 1Hello, World, and What Actually HappenedYou typed four lines in chapter 0 and a computer said hello back. Now find out what every single character in them was for, and print something of your own.Project: A fortune card
  2. 2Your First Error MessageYou are going to see thousands of these. Most people learn to read them by accident, slowly, over years. Here is the short version, covering the shape of a message, which one to read first, and the six you'll meet this week.Project: Fix the broken card
  3. 3Variables, and the Types They Insist OnChapter 1's program said the same thing every time. Here is how a program holds on to something, and why C++ makes you announce, up front, what kind of thing you are holding.Project: A receipt
  4. 4Strings: Text That Doesn't Fight BackIn most languages text is easy. In C++ it used to be famously awful, and then std::string arrived. Here is how to join it, measure it, cut pieces out of it, and find things inside it.Project: An initials formatter
  5. 5Input: Letting the Reader Talk BackEvery program so far has said the same thing every time it ran. This is where it starts listening, and where C++ sets its most famous trap for beginners, which you will meet on purpose rather than at midnight.Project: Mad-libs
  6. 6Arithmetic, and Where It Betrays YouArithmetic looks like the safe part of a programming language. It isn't. C++ will quietly give you 3 when you expected 3.5, and it will do it without a warning, because as far as it is concerned you asked for exactly that.Project: Seconds into hours, minutes and seconds
  7. 7Deciding Things: if, else, and BooleansEvery program so far has run every line, top to bottom, no matter what. This is where a program starts choosing, and where three earlier chapters finally get to keep their promises.Project: Ticket pricing with real rules
  8. 8Loops: Doing It Again Until It's DoneA program that can repeat itself is a different kind of thing from one that runs top to bottom. This chapter also covers what to do when a loop never stops, because yours will, probably today.Project: The number guessing game
  9. 9Functions: Giving Work a NameYou have been copying and pasting since chapter 1. This chapter gives a name to a piece of work so you can ask for it by name instead, which turns out to change how you write everything after it.Project: The guessing game, rebuilt, then a pair of dice
  10. 10std::vector: A List That GrowsEvery program so far has held one of each thing. This chapter gives you a container that holds as many as you like and grows as you fill it, which is the point where the programs stop being toys.Project: A thousand dice
  11. 11Printing, Poking, and Proving It WorksA program that crashes tells you where to look. A program that quietly returns the wrong number tells you nothing. This chapter is about the second kind, which is the kind you will spend your time on.Project: Hunt the planted bug
  12. 12Copies, References, and Not Wasting WorkChapter 9 said a function gets a copy of what you pass it, and left it there. This chapter is the rest of that sentence, covering how to let a function change your variable, and how to stop copying things that are expensive to copy.Project: Text statistics
  13. 13Sorting and Searching Without Writing EitherSorting a list is a famously fiddly thing to write, and you are never going to write one. This chapter is about the pile of ready-made algorithms C++ ships with, and how to read the slightly odd way they are called.Project: The high-score table
  14. 14struct: Things That Belong TogetherUp to now you have used types other people designed. This chapter is where you make one, by gluing a few existing types together and giving the result a name, which turns out to be most of what designing a program is.Project: The contact book
  15. 15Files: Making Your Program RememberEverything your programs have made so far died the moment they exited. This chapter fixes that with two new types that behave almost exactly like cout and cin, plus one trap that can eat your data before you have written a byte.Project: A to-do list that survives quitting
  16. 16Maps: Looking Things Up by NameA vector is a list you look through. A map is a list you look things up in, by name or by word or by anything you like, and it turns a loop you would have written into a pair of square brackets.Project: Word frequency
  17. 17Validation: Assuming the WorstThis chapter pays off three promises made much earlier, covering why while (cin >> x) works, how to recover from a reader typing nonsense, and what to do when a function has no honest answer to give you.Project: The unbreakable to-do list
  18. 18Classes: Types You Design YourselfChapter 14 built types out of fields. This chapter puts the functions inside them too, and then takes away the reader's ability to reach in and break things, which sounds unfriendly and is the entire point.Project: A deck of cards
  19. 19Constructors, and Types That Can't Be BrokenChapter 18's Deck was only correct if somebody remembered to call fill on it. A constructor removes the remembering. It is code that runs when the object is made, so there is no moment at which a half-built one exists.Project: War
  20. 20Splitting a Program Across FilesEvery program so far has lived in one file, and chapter 14's contact book was already straining. This chapter splits it up, which means meeting the linker properly, and finding out what the compiler was doing with those include lines all along.Project: Split the contact book
  21. 21Interlude: Build Something RealTwenty chapters of pieces, and one program that uses nearly all of them. Nothing here is new. This is the chapter where you find out how much you can already do, and where Part 1 ends.Project: The expense tracker

Part 2: Lifting the floor

Back to the machinery the standard library was handling for you.

  1. 22What a Variable Really IslockedPart 1 taught you to use variables without ever asking where they are. This chapter answers that, and the answer turns out to be the foundation everything in Part 2 is built on.Project: Map your own memory
  2. 23Pointers, FinallylockedChapter 22 let you print an address but gave you nowhere to keep one. A pointer is a variable that holds an address, and almost everything difficult about C++ starts here.Project: Break a pointer on purpose
  3. 24What std::string Was HidinglockedA std::string is 24 bytes whether it holds one letter or a thousand. This chapter explains how that can possibly be true, and what text looked like before anyone made it comfortable.Project: Walk a string by hand
  4. 25Raw Arrays, and Why vector ExistslockedChapter 24 showed you text without std::string. This is numbers without std::vector, which is where you find out what push_back and .size() were actually protecting you from.Project: Write past the end and find out
  5. 26The Heap, new, delete, and What Goes WronglockedEverything so far has been handed to you and taken away at a closing brace. This chapter is about memory you ask for and must give back, why it exists, and the three ways it ruins programs.Project: Break the heap three ways
  6. 27Ownership, Destructors Doing the WorklockedChapter 26 left you responsible for remembering a delete on every path out of every function. This chapter hands that job to the language, and explains why you never had to close a file.Project: Watch things clean up after themselves
  7. 28Templates, One Function, Any TypelockedChapter 10 handed you std::vector<int> and asked you to take the angle brackets on trust. This is the promise being kept, and it turns out to explain the worst error messages in C++ as well.Project: Write the code the compiler writes
  8. 29Iterators, and What range-for BecomeslockedChapter 13 told you to write v.begin(), v.end() and not ask questions. This is the answer, and it turns out range-for, std::find and std::sort are all the same idea wearing different clothes.Project: Take the loop apart
  9. 30Borrowing Without OwninglockedReferences, pointers and string_view all let you use something without owning it, and all three break the same way. This chapter is about the one question that decides whether your code is correct, which is how long the thing you borrowed lives.Project: Four ways to dangle
  10. 31Undefined Behaviour, When C++ Stops PromisinglockedChapter 6 promised an explanation and every chapter since has quietly added to the list. Here is what undefined behaviour actually means, why it is worse than a crash, and the tools that turn it into one.Project: Point a sanitizer at everything you broke
  11. 32Where to Go NextlockedThe last chapter. What this book left out and where to learn it, how to read the documentation without drowning, what the newer standards changed, and how to get better at this from here.Project: Read somebody else's program