Concepts: How C++ Finally Got Compile-Time Constraints on Templates

Bjarne Stroustrup explains what C++ concepts are, why they are real and shippable today, and why it took decades to get them right.

BSBjarne StroustrupGast
Kanal
Lex Fridman Podcast
Video
10:24
Lesezeit
4 Min.
Original
Englisch
Auf YouTube öffnen
Beim Scrollen schrumpft das Video in die rechte untere Ecke und läuft weiter. Untertitel folgen Ihrer Lesesprache.
LesespracheIn 1 Sprachen verfügbar
Diesen Artikel gibt es noch nicht auf Deutsch, daher wird das Original (Englisch) angezeigt.
ZUSAMMENFASSUNG

In 30 Sekunden

Stroustrup describes concepts as compile-time predicates that state what a generic component requires of its parameters, such as what it means for something to be sortable. He says he wanted templates to be flexible, as fast as hand-written code and properly type-checked, and that earlier attempts at concepts failed because they did not fit C++ or its performance demands.

  1. 1Stroustrup says concepts are compile-time predicates that ask a type whether it has the properties a generic component needs, such as a begin and end, random access and a less-than operator.
  2. 2Concepts are not science fiction: working versions have existed for four or five years, they are part of C++20 and its standard library, and major compilers support them.
  3. 3The term comes from Alexander Stepanov, the father of generic programming, who called them concepts because they are the fundamental concepts of an area.
  4. 4Stroustrup wanted templates to be flexible, as fast as hand-written code, and properly type-checked, and for decades he and others did not know how to get all three at once.
  5. 5The earlier attempt around 2003 stumbled because it did not handle implicit conversions and mixed-type arithmetic well, compiled slowly, and could not meet C++'s performance requirements without optimizers beyond the state of the art.
TRANSKRIPT

Das vollständige Transkript nach Kapiteln

01

Concepts are compile-time predicates, and you can use them today

Stroustrup starts by stressing that concepts are real: they did not exist ten years ago, but versions that work have been around for four or five years. The design came from Gabriel Dos Reis and himself, who were professors and postdocs in Texas at the time. It is part of C++20 along with the standard library components that use it, and it is available in clang and GCC, with Microsoft expected to follow as C++20 arrives in all major compilers.

He explains the basic idea with a sort function. Sorting needs at least two parameters: a data structure with a given type and a comparison criterion, and the two are related, since you can't compare things without knowing their type. To say something is sortable, you look up the standard's definition: it has to be a sequence with a beginning and an end, offer random access, and have element types that can be compared.

Concepts, he says, are compile-time predicates that ask exactly those questions. Are you a sequence, with a begin and an end? Are you a random-access sequence, with subscripting and addition? Does your element type have a less-than? Instead of accepting a parameter of any type, the function says it takes something sortable. The comparison criterion can be less-than, greater-than or something you invent, and it is written to operate on the element type.

02

The word comes from Stepanov, and C already had the idea informally

Stroustrup notes that the word concept was used by Alexander Stepanov, whom he calls the father of generic programming in the context of C++. Stepanov called them concepts because they are the fundamental concepts of an area, and so they should carry that name.

The idea itself has always existed, he argues. If you look at the K&R book on C, it describes arithmetic types and integral types, lists what they are and states their properties. What is new in C++ is that you can write a concept that asks a type whether it has the properties necessary to be integral, for instance whether it supports operations like multiply and divide.

03

Getting flexibility, speed and type checking together took decades

Lex Fridman asks why concepts did not arrive in C++11 and what made the idea so hard, going back to templates in the late 1980s. Stroustrup says he wanted three things from templates. They had to be flexible enough to express things he could not imagine, because he disliked languages that force you into only what the designer thought good. They also had to run as fast as or faster than hand-written code, and to have proper type checking of interfaces.

Neither he nor anyone else knew how to get all three, and he thought C++ must have the first two or it would not be C++. It bothered him for a couple of decades, especially since he had put function argument type checking into C and valued good interfaces. A renewed attempt came around 2003, when he and Dos Reis analyzed the problem and possible solutions, and a group at Indiana University began a project.

They thought a good system could be done in a couple of years and make it into C++11, but he now thinks many of the fundamental ideas were wrong. They were unconventional for C++, handled implicit conversions and mixed-type arithmetic poorly, and drew heavily from the functional community, which treats multiple types differently and lacks C++'s draconian performance requirements.

In the end, he says, it was too slow to compile, too hard to use, and did not run fast enough without optimizers beyond the state of the art. The approach treated a set of parameters as defining a set of operations reached through an indirection table, like virtual functions, and relied on optimizing that indirection away. They could not make that work, so they had to do something else.

Mehr aus diesem Kanal

3:36:56EN
Lex Fridman· 16. Sept. 2026PodcastPsychiatry Has Never Found Its PenicillinModeration: Lex FridmanHistorian Andrew Scull tells Lex Fridman why the field can ease symptoms, has never cured a major mental illness, and keeps paying for overconfidence with patients' lives.Mental HealthPublic HealthNeuroscience
5:15:51EN
Lex Fridman· 26. Aug. 2026Podcast · 2 StimmenDHH Did Not Write a Line of Omarchy QuattroGast: DHH · Moderation: Lex FridmanThirteen months after a skeptical conversation with Lex Fridman, the Rails creator describes how Opus 4.5, a swarm of coding agents, and a one-minute Linux installer ended his era of hand-chiseled Ruby.Coding-AgentenVibe CodingAgent Engineering+3
3:12:58EN
Lex Fridman· 12. Aug. 2026Podcast · 2 StimmenOne Cold Shower, Twenty Champions: How Khabib Nurmagomedov Was ForgedGast: Khabib Nurmagomedov · Moderation: Lex FridmanIn a Makhachkala school gym, seventy men shared one shower, and the water was only cold.Martial ArtsCombat SportsCoaching+3
3:46:18EN
Lex Fridman· 28. Juli 2026Podcast · 2 StimmenIf You Take Slavery Out of the Picture, There Is No Civil WarGast: Gary Gallagher · Moderation: Lex FridmanIn 1860, enslaved people were worth more than every railroad and factory in the United States combined.Political PhilosophyLeadershipHistory
3:51:47EN
Lex Fridman· 30. Juni 2026Podcast · 2 StimmenForty-Six Percent of Emperors Were Overthrown. The Empire Lasted AnywayGast: Anthony Kaldellis · Moderation: Lex FridmanHistorian Anthony Kaldellis walks Lex Fridman through 2,200 years of Roman history and argues that what we call Byzantium was Rome itself.Political PhilosophyGeopoliticsLeadership
2:53:42EN
Lex Fridman· 29. Mai 2026Podcast · 2 StimmenThe Moon Is Falling. It Just Keeps Missing.Gast: Biggest Mysteries in Physics · Moderation: Lex FridmanFermilab physicist Don Lincoln tells Lex Fridman that the history of physics is a history of unification, and that antimatter, dark energy, and a theory of everything still sit far beyond the experiments we can run.Quantum MechanicsScience CommunicationPhilosophy of Science