Abbreviated Function Templates
Explore how C++20 introduces abbreviated function templates that allow function declarations with auto or concept constraints to become function templates automatically. Understand the syntax, usage of concepts like std::integral, and how overloading works with abbreviated templates to write clearer and type-safe code.
We'll cover the following...
With C++20, you can use an unconstrained placeholder (auto) or a constrained placeholder (concept) in a function declaration, and this function declaration automatically becomes a function template.
The definitions of the function templates gcd (line 4), gcd1 (line 11), and gcd2 (line 17) are the ones I already presented in section Four Ways to Use a Concept. gcd uses a requires clause, gcd1 a trailing requires clause, and gcd2 a constrained template parameter.
Now to something new. Function template gcd3 has the concept std::integral as a type parameter and becomes, therefore, ...