Skip to content

Latest commit

 

History

History
65 lines (55 loc) · 1.12 KB

abbreviated_function_template.md

File metadata and controls

65 lines (55 loc) · 1.12 KB

Abbreviated Function Template

Generic lambdas were introduced in C++14, where auto was used as a function parameter.

C++14
// lambda definition
auto addTwo = [](auto first, auto second)
{
    return first + second; 
};

In C++20, similarly to how auto was used in lambdas, auto can now be used as a function parameter to get a function template.
C++20 has added abbreviated function templates, used to shorten the template version of the function.

C++ C++20
template< typename T >
T addTwo(T first, T second)
{
     return first + second; 
}
// this is a template, without the word 'template'!
auto addTwo(auto first, auto second)
{
    return first + second;  
}

(To be clear - it is the auto used as a param - even just one of the params - that makes it a template.)

Looking at it from the point of view of Concepts, auto acts like the most unconstrained Concept.