Predefined Concepts - Part I
Explore the essential predefined concepts in C++20 that enhance template programming and type safety. Understand language-related, arithmetic, lifetime, comparison, object, and callable concepts to write clearer, more generic code. This lesson covers the implementation and usage of these standard concepts to help you apply them effectively.
The golden rule “Don’t reinvent the wheel” also applies to concepts. The C++ Core Guidelines are very clear about this rule:
T.11: Whenever possible use standard concepts.
Consequently, I want to give you an overview of the important predefined concepts. I intentionally ignore any special or auxiliary concepts.
All predefined concepts are detailed in the latest C++20 working draft, N4860, and finding them all can be quite a challenge! Most of the concepts are in the concepts library and ranges library. Additionally, a few concepts are in the language support library, general utilities library, iterators library, and numerics library. The C++20 draft N4860 also has an index to all library concepts and shows how the concepts are implemented.
Language support library
This section discusses an interesting concept, three_way_comparable. It is used to support the three-way comparison operator. It is specified in the header <compare>.
More formally, let a and b be values of type T. These values are three_way_comparable only if:
- (a <=> b == 0) ==