C++ Expert · Lesson 1 · On the house

concepts

Constrain a template (C++20). Lesson 1 is free — on the house.

Lessons · C++ Expert · Lesson 1 of 10 · On the house

concepts

Constrain a template (C++20).

Illustration for the C++ course
Lesson 1 is on the house. The rest opens with the paper, the Daily, or a gift.

Expert assumes Introductory through Medium. We go deep — no beginner restarts.

C++20 concepts name requirements on template parameters. Instead of failing deep inside an instantiation with a novel-length error, the constraint is checked up front. You write readable intent: this T must be addable, sortable, a range, etc. When the compiler or runtime complains, read the first error first — later errors are often fallout. Change one thing. Rebuild. That calm loop beats rewriting the whole file from memory. Templates are compile-time machinery. The payoff is reusable code; the cost is error messages that read like a novel. Start from a tiny example you can instantiate by hand. Before you build, predict what concepts should do on the still's inputs. Prediction turns compiling into confirmation.

template concept Addable = requires(T a, T b) { a + b; }; defines a concept Addable that holds when the requires-expression is valid. Use it as template or with requires Addable. Braces mark scope in C++. A missing brace or an if without braces that later gains a second statement is a classic footgun — prefer braces even for one-liners while you're learning. Templates are compile-time machinery. The payoff is reusable code; the cost is error messages that read like a novel. Start from a tiny example you can instantiate by hand. Before you build, predict what concepts should do on the still's inputs. Prediction turns compiling into confirmation.

requires expressions can demand nested types, expressions, and { expr } -> convertible_to style return constraints. Start small: one operation you actually need. Don't invent a mega-concept that restates half the standard. Braces mark scope in C++. A missing brace or an if without braces that later gains a second statement is a classic footgun — prefer braces even for one-liners while you're learning. Templates are compile-time machinery. The payoff is reusable code; the cost is error messages that read like a novel. Start from a tiny example you can instantiate by hand. Before you build, predict what concepts should do on the still's inputs. Prediction turns compiling into confirmation.

Standard concepts in and cover a lot: integral, same_as, convertible_to, range, sortable. Prefer them before rolling your own when they match. Templates are compile-time machinery. The payoff is reusable code; the cost is error messages that read like a novel. Start from a tiny example you can instantiate by hand. Before you build, predict what concepts should do on the still's inputs. Prediction turns compiling into confirmation.

Concepts don't replace tests or careful design — they document compile-time contracts. Over-constraining blocks legitimate uses; under-constraining returns you to cryptic errors. Iterate on real call sites. When the compiler or runtime complains, read the first error first — later errors are often fallout. Change one thing. Rebuild. That calm loop beats rewriting the whole file from memory. Braces mark scope in C++. A missing brace or an if without braces that later gains a second statement is a classic footgun — prefer braces even for one-liners while you're learning. Before you build, predict what concepts should do on the still's inputs. Prediction turns compiling into confirmation.

The still is the smallest honest concept: Addable means a + b is valid for T. Instantiating a template constrained on Addable with a type that can't add fails early and clearly. Templates are compile-time machinery. The payoff is reusable code; the cost is error messages that read like a novel. Start from a tiny example you can instantiate by hand. const is a promise you make to the compiler and to your future self. Prefer it by default on values and references you don't intend to mutate — the type system will catch slip-ups. Before you build, predict what concepts should do on the still's inputs. Prediction turns compiling into confirmation.

Put concepts in a sentence you could teach a friend: what changes in the program, what stays the same, and what you expect when you build and run the still. Zero-based indexing is the tax you pay once and forever. When something looks one-off, count from zero on paper before you blame the standard library. Templates are compile-time machinery. The payoff is reusable code; the cost is error messages that read like a novel. Start from a tiny example you can instantiate by hand. Before you build, predict what concepts should do on the still's inputs. Prediction turns compiling into confirmation.

If you are skimming, stop and re-read the still's lines in order while pointing at each token. concepts is not a vocabulary flashcard — it is a behavior you should be able to simulate on paper.

concept

template <typename T>
concept Addable = requires(T a, T b) { a + b; };
Named constraint via requires.

Introduce type parameter T, then concept Addable as a requires-expression demanding a + b. Types for which that expression is ill-formed do not satisfy Addable. Use the concept on template APIs to gate instantiation.

Pitfalls: concepts that are too vague or too narrow; forgetting C++20 mode (-std=c++20); writing requires that accidentally force copies; assuming concepts run at runtime — they don't. Constrain early, instantiate less pain.

Quiz

What do concepts constrain?

Quiz

What does requires(T a, T b) { a + b; } mean?

Quiz

Which standard introduced concepts?

Quiz

When are concepts checked?

Quiz

Why prefer concepts over unconstrained templates?

Quiz

Where do many standard concepts live?

Check

Match the still for: concepts.

Constraints have names now. Next: ranges — views over sequences.

Open-book: the answers are on this page. Pass every quiz and check (7) to mark the lesson done. This visit: 0/7.

concepts · C++ Expert (free) — The Gold Standard