C++20 Concepts: part 4
In this issue of the concepts tutorial, I’ll discuss in detail about abbreviated function template syntax , constraining auto
with abbreviated function template syntax , constraining deduced return type, i.e. constraining the auto
return type with concepts.
1. Abbreviated function template syntax in C++20
1.1. Introduction
With the introduction of concepts in C++20, generic program (and here function templates) have changed in meaning and specification. Abbreviated function templates are a part of the change.
C++20 abbreviated function templates in C++20 mean the same to generic functions, what generic lambda expressions in C++14 meant: use of auto
in function parameters instead of writing the verbose template syntax . For e.g.
template <typename T>
void foo(T param) { /* do something */ } (1)
in C++20 can be written as
void foo(auto param) { /* do something */ } (2)
without changing the meaning of the function. Here’s what cpp reference page have to say about abbreviated function template syntax:
When placeholder types (either auto …) appear in the parameter list of a function declaration or of a function template declaration, the declaration declares a function template …
which simply means that the definitions (1)
and (2)
are equivalent. This applies to all kinds of template declarations including variadic templates.
This declaration may remind one of the generic lambdas which were introduced in C++14 which allowed auto
in the parameter list of the lambda definition and were further enhance in C++20 with the introduction of template syntax in lambda expression .
1.2. Difference between non-type template with auto
and abbreviated template syntax
Abbreviated function template syntax, as the name suggest are very different from the non-type template parameter with auto
syntax, a feature that was introduced in C++17 and allowed for definition of generic C++ objects and not functions with auto
.