Using concepts to constrain auto parameters
In Chapter 2, Template Fundamentals, we discussed generic lambdas, introduced in C++14, as well as lambda templates, introduced in C++20. A lambda that uses the auto
specifier for at least one parameter is called a generic lambda. The function object generated by the compiler will have a templated call operator. Here is an example to refresh your memory:
auto lsum = [](auto a, auto b) {return a + b; };
The C++20 standard generalizes this feature for all functions. You can use the auto
specifier in the function parameter list. This has the effect of transforming the function into a template function. Here is an example:
auto add(auto a, auto b) { return a + b; }
This is a function that takes two parameters and returns their sum (or to be more precise, the result of applying operator+
on the two values). Such a function using auto
for function parameters is called an abbreviated function template. It is basically...