C++11 allowed lambda functions to deduce the return type based on the type of the expression given to the return statement. The C++14 standard provides return type deduction in all functions, templates, and lambdas. C++14 allows return type deduction for functions that are not of the form return expressions. In this post, we explain the auto
keyword, what is an auto type deduction, and how we can use it in functions, templates, and lambdas. Here are some very simple examples.
What is the auto keyword in C++?
The auto
keyword arrives with the new features of the C++11 standard and above. It can be used as a placeholder type specifier (an auto-typed variable), or it can be used in a function declaration, or a structured binding declaration. The auto
keyword can be used with other new CLANG standards like C++14, C++17, etc. Here is a simple example of how to use auto-typed variables in C++.
1 2 3 4 5 |
unsigned long int L; auto a = L; // a is automatically unsigned long int |
The auto keyword was being used as an automatic data specifier (storage class specifier) until C++11. This feature was removed by the C++11 standard.
What is auto return type deduction in C++?
In object oriented programming, type inference or deduction means the automatic detection of the data type of an expression and its conversion to a new type in a programming language and auto return type deduction may deduce return. type (i.e. float
parameters to int
return values). By the C++14 standard, we can use auto return type deduction in functions, templates, in lambdas. Now let’s see some simple examples that show how we can use them. If you want to use return type deduction in functions, templates, and lambdas, it must be declared with auto
as the return type, but without the trailing return type specifier in C++11.
The auto return type deduction feature can be used with parameter types in functions. Here is a simple example,
1 2 3 4 5 6 |
auto sq(int r) // auto return type deduction in function { return r*r; } |
The auto return type deduction feature can be used on modified parameters. Here is a simple example.
1 2 3 4 5 6 |
auto inc(int r) // auto return type deduction in function { return ++r; } |
The auto return type deduction feature can be used with references in functions.
1 2 3 4 5 6 7 |
auto& zero(int& r) // auto return type deduction in function { r = 0; return r; } |
The auto return type deduction can be used with templates. Here is an example:
1 2 3 4 5 6 |
template <typename T> auto template_sq(T t) // auto return type deduction in template { return (int)(t*t); // deduce to int } |
The auto
keyword is very useful in lambda declarations and it has an auto return type deduction feature too. See a simple lambda example below.
1 2 3 4 5 6 |
auto lambda_sq = [](int r) // auto return type deduction in lambda { return r*r; }; |
Note that one of the important differences between lambdas and normal functions is that normal functions can refer to themselves by name but lambdas cannot.
Is there a full example of auto return type deduction in C++?
Here is a full example about auto return type deduction in functions, a template and a lambda.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> auto sq(int r) // auto return type deduction in function { return r*r; } auto inc(int r) // auto return type deduction in function { return ++r; } auto& zero(int& r) // auto return type deduction in function { r = 0; return r; } template <typename T> auto template_sq(T t) // auto return type deduction in template { return (int)(t*t); // deduce to int } auto lambda_sq = [](auto r) // auto return type deduction in lambda { return (int)(r*r); // deduce to int }; int main() { int x = 4; double y = 5.01; double z = 5.90; std::cout << "x2:" << sq(x) << std::endl; std::cout << "y2:" << sq(y) << std::endl; std::cout << "z2:" << sq(z) << std::endl; std::cout << "inc:" << inc(10) << std::endl; auto &c = zero(x); std::cout << "zero:" << x << std::endl; auto t = template_sq(y); std::cout << "template:" << t << std::endl; auto l = lambda_sq(y); std::cout << "lambda:" << l << std::endl; system("pause"); return 0; } |
and the output will be as follows:
1 2 3 4 5 6 7 8 9 |
x2:16 y2:25 z2:25 inc:11 zero:0 template:25 lambda:25 |
If you need more examples, here is another example that is used with class and struct properties,
The return type deduction for normal functions is explained in this paper https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3638.html
This auto feature is supported by the Clang-enhanced C++ compilers too. For more information on this feature, please see the C++11 standard Proposal Document at: Multi-declarator auto
You can use decltype(auto)
keyword as a return type deduction in some C++ compilers. In C++20, you can use Arithmetic
expression with auto (Arithmetic auto). In C++20, arithmetic types are allowed in the function templates that uses arithmetics.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here