Lecture, Keynote
Bjarne Stroustrup: C++ Concepts - Constraints on Template Parameters
- Concepts were designed by Gabby Dos Reis, Andrew Sutton, and the speaker while they were professors and postdocs in Texas.
- The implementation by Andrew Sutton has been commercially available for four to five years prior to the transcript's recording.
- Concepts were standardized as part of C++20 and include a dedicated standard library implementation.
- The feature is currently implemented in Clang and GCC, with Microsoft expected to include support soon.
- All major compilers are anticipated to support the full C++20 standard in 2020.
- Concrete production uses of concepts exist, dispelling the notion that the technology is theoretical.
- Concepts function as compile-time predicates that verify if a type satisfies specific structural requirements before compilation.
- A sort function example demonstrates that "sortable" types must be sequences with begin/end iterators and random access capabilities.
- Element types in a sortable sequence must possess a "less than" operator or a defined logical comparison operation.
- Developers can define custom parameters for sort criteria, such as using "greater than" or user-defined comparison logic.
- The term "concept" in this context was adopted from Alex Stepanov, considered the father of generic programming in C++.
- Alex Stepanov originally named the feature "concepts" to denote the fundamental principles governing a specific area of programming.
- Historically, C has implicitly used concepts by defining arithmetic and integral types with specific properties like addition and subtraction.
- The speaker began designing C++ templates in 1987 with three primary objectives: extreme flexibility, performance parity with handwritten code, and rigorous interface type checking.
- While flexibility and performance were achieved in early templates, satisfying the third goal of explicit constraint expression proved impossible for two decades.
- In 2002–2003, the speaker and Gabby Desraies revisited the problem, collaborating with a group at the University of Indiana.
- Initial 2000s proposals for concepts were rejected because they were too conventional and failed to address C++'s implicit conversion and mixed-type arithmetic requirements.
- Early concept designs borrowed heavily from the functional programming community, which differs from C++ in handling multiple types simultaneously.
- Previous implementations failed to compile quickly enough, were difficult to use, and could not achieve necessary performance without state-of-the-art optimizers.
- Early attempts relied on indirect indirection tables similar to virtual functions, a strategy that ultimately could not eliminate runtime overhead.
- The final design avoids indirection tables, enabling concepts to serve as direct compile-time constraints without performance penalties.