Abbreviated Function Templates in C++ 20
Last Updated :
06 Sep, 2023
Abbreviated Function Template is a new feature introduced in C++20 to simplify the syntax of function template definition. Traditionally, we use function templates to define a function that can take parameters of different types. But sometimes, it might get complicated and hard to understand. In these cases, we can implement an abbreviated function template to make the function definition clear and concise.
An abreviated function template are the function defintion that contains auto for the parameter type instead of template defined type.
Syntax
return_type function_name ( auto paramter_name);
The above is a simple example of an abbreviated function template. It can vary depending on different contents such as the number of parameters, their mutability, etc.
Example
C++
#include <iostream>
using namespace std;
auto getsum( auto value1, auto value2)
{
return value1 + value2;
}
int main()
{
double result = getsum(3.5, 6.2);
cout << "The sum is: " << result << endl;
return 0;
}
|
Output
The sum is: 9.7
Constrained auto for Abbreviated Function Templates
The problem with Abbreviated Function Templates is that it accepts all types of parameters even if the code is not suitable for that particular type. The solution to this is to use the concepts to define the accepted type of parameters.
Syntax
return_type function_name ( C auto paramter_name);
where C is the concept defined somewhere in the program.
Example
Trying to run the above code using concepts that only permit integral values.
C++
#include <iostream>
using namespace std;
template < typename T>
concept C = is_integral_v<T>;
auto getsum(C auto value1, C auto value2)
{
return value1 + value2;
}
int main()
{
double result = getsum(3.5, 6.2);
cout << "The sum is: " << result << endl;
return 0;
}
|
Output
main.cpp: In function ‘int main()’:
main.cpp:17:27: error: no matching function for call to ‘getsum(double, double)’
17 | double result = getsum(3.5, 6.2);
| ~~~~~~^~~~~~~~~~
main.cpp:9:6: note: candidate: ‘template requires (C) && (C) auto getsum(auto:11, auto:12)’
9 | auto getsum(C auto value1, C auto value2)
| ^~~~~~
main.cpp:9:6: note: template argument deduction/substitution failed:
main.cpp:9:6: note: constraints not satisfied
main.cpp: In substitution of ‘template requires (C) && (C) auto getsum(auto:11, auto:12) [with auto:11 = double; auto:12 = double]’:
main.cpp:17:27: required from here
main.cpp:7:9: required for the satisfaction of ‘C’ [with auto:11 = double]
main.cpp:7:13: note: the expression ‘is_integral_v [with T = double]’ evaluated to ‘false’
7 | concept C = is_integral_v<T>;
| ^~~~~~~~~~~~~~~~
The above code shows an error because concept C does not permit double values. It will run fine if the arguments are of type int.
Benefits of Abbreviated Function Templates
- Reduced code verbosity: It reduces the code verbosity because we are using the auto keywords which automatically deduce the type of the arguments.
- Improved code maintenance: It is easy to maintain and make changes in the template without the need to manually change the type declarations.
Share your thoughts in the comments
Please Login to comment...