What Is Auto Return Type Deduction In C++?

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++.

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,

The auto return type deduction feature can be used on modified parameters. Here is a simple example.

The auto return type deduction feature can be used with references in functions.

The auto return type deduction can be used with templates. Here is an example:

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.

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.

and the output will be as follows:

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.

What Is Auto Return Type Deduction In C++ The C++ Builder Logo

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