Features of C++ 20
C++ has a tradition of introducing new improvements and features in every 3 years in the form of a standard. With the last standard having released in 2017 as C++ 17, C++20 is going to be the latest standard. Below ere are some major features in C++ 20:
- C++ Concepts library
- 3-way comparisons
- Map contains
- Range-based for loop
- New identifiers ( import, module)
- Calendar and time zone library
- std::string functions
- Array bounded/unbounded
- Likely and unlikely attributes
C++ Concepts library:
The concepts library provides definitions of fundamental library concepts that can be used to perform compile-time validation of template arguments and perform function dispatch based on properties of types. These concepts provide a base for educational reasoning in programs.
This section has around 15 concepts that should be self-explanatory. These concepts express relationships between types, type classifications, and fundamental type properties.
Defined in header <concepts> Defined in namespace std
- integral: Specifies that a type is an integral type.
- signed_integral: Specifies that a type is an integral type that is signed.
- unsigned_integral: Specifies that a type is an integral type that is unsigned.
- floating_point: Specifies that a type is a floating-point type.
- same_as: Specifies that a type is the same as another type.
- derived_from: Specifies that a type is derived from another type.
- convertible_to: Specifies that a type is implicitly convertible to another type.
- common_with: Specifies that two types share a common type.
concept integral = is_integral_v<T>;
concept signed_integral = integral<T> && is_signed_v<T>;
concept unsigned_integral = integral<T> && !signed_integral<T>;
concept floating_point = is_floating_point_v<T>;
The three-way comparison operator expressions is of the form:
lhs <=> rhs
The spaceship operator looks like <=> and its official C++ name is the 3-way comparison operator. It is called so because it is used by comparing two objects, then comparing that result with 0:
(x <=> y) < 0 is true if x < y (x <=> y) > 0 is true if x > y (x <=> y) == 0 is true if x and y are equal/equivalent.
The 3-way comparison operator not only let express orderings and equality between objects but also the characteristics of the relations. The spaceship operator is a very welcome addition to C++. It gives us more expressiveness in how to define our relations, allows us to write less code to define them, and avoids some performance pitfalls of manually implementing some comparison operators in terms of others.
std::map<Key, T, Compare, Allocator>::contains
It gives a much easier way to check if a key is present in the associative container(set or map) in C++20. It replaces the find built-in function.
Range-based for loop with Initialization:
The range-based for loop changed in C++17 to allow the begin() and end() expressions to be of different types and in C++20, an init-statement is introduced for initializing the variables in the loop-scope. It allows us to initialize the container we wish to loop through in the range-declaration itself.
for (init-statement(optional) range_declaration : range_expression)
/* loop body */
New identifiers(import, module):
- Modules help to divide large amounts of code into logical parts. Modules promise faster compile times, isolation of macros, and make header files redundant.
- They express the logical structure of the code and helps in getting rid of the ugly macro workaround. Modules are orthogonal to namespaces. It makes no difference in which order you import a module.
- Modules enable you to express the logical structure of your code. You can explicitly specify names that should be exported or not. Additionally, you can bundle a few modules into a bigger module and you can provide them as a logical package.
- With modules, there is now no need to separate your source code into an interface and an implementation part.
Calendar and time zone library:
The chrono library of C++ 11/14 was extended with a calendar and time-zone feature. The calendar consists of types, which represent a year, a month, a day of a weekday, or n-th weekday of a month. These elementary types can be combined with complex types such for example year_month, year_month_day, year_month_day_last, years_month_weekday, and year_month_weekday_last. The operator “/” is overloaded for the convenient specification of time points. Additionally, we will get with C++20 new literals: d for a day and y for a year.
Due to the extended chrono library, the following functions are easy to implement:
- Get the last day of a month.
- Get the number of days between two dates.
- Printing the current time in various time-zones.
Defined in header <chrono> Defined in namespace std::chrono
auto date1 = 2020y/sep/8; auto date2 = 21d/oct/2018; auto date3 = jan/27/2019;
ends_with(“suffix”): Checks if the string ends with the given suffix.
starts_with(“prefix”) :Checks if the string view starts with the given prefix.
- It checks whether T is an array type of unknown bound and provides the member constant value which is equal to true, if T is an array type of unknown bound. Otherwise, value is equal to false.
- It checks whether T is an array type of known bound and provides the member constant value which is equal to true, if T is an array type of known bound. Otherwise, value is equal to false.
It converts the given array/”array-like” object to an std::array. It creates an std::array from the one-dimensional built-in array a. The elements of the std::array is copy-initialized from the corresponding element of a. Copying or moving a multidimensional built-in array is not supported.
likely and unlikely attributes:
It provides a hint to the Optimizer that the labeled statement is likely/unlikely to have its body executed. Both attributes allow giving the Optimizer a hint, whether the path of execution is more or less likely.