8.8 — Type deduction for functions

Consider the following program:

When this function is compiled, the compiler will determine that x + y evaluates to an int, then ensure that type of the return value matches the declared return type of the function (or that the return value type can be converted to the declared return type).

Since the compiler already has to deduce the return type from the return statement, in C++14, the auto keyword was extended to do function return type deduction. This works by using the auto keyword in place of the function’s return type.

For example:

Because the return statement is returning an int value, the compiler will deduce that the return type of this function is int.

When using an auto return type, all return values must be of the same type, otherwise an error will result. For example:

In the above function, the two return statements return values of different types, so the compiler will give an error. If such a case is desired for some reason, either static_cast some or all of the return values so they are the same type, or explicitly specify a return type for your function.

Unlike type deduction for objects, there isn’t as much consensus on best practices for function return type deduction. When using type deduction with objects, the initializer is always present as part of the same statement, so it’s usually not overly burdensome to determine what type will be deduced. With functions, that is not the case -- when looking at a function’s prototype, there is no context to help indicate what type the function returns. A good programming IDE should make clear what the deduced type of the function is, but in absence of having that available, a user would actually have to dig into the function body itself to determine what type the function returned. The odds of mistakes being made are higher.

Best practice

Favor explicit return types over function return type deduction for normal functions.

Trailing return type syntax

The auto keyword can also be used to declare functions using a trailing return syntax, where the return type is specified after the rest of the function prototype.

Consider the following function:

Using the trailing return syntax, this could be equivalently written as:

In this case, auto does not perform type deduction -- it is just part of the syntax to use a trailing return type.

Why would you want to use this?

One nice thing is that it makes all of your function names line up:

The trailing return syntax is also required for some advanced features of C++, such as lambdas (which we cover in lesson %Failed lesson reference, id 9367%).

For now, we recommend the continued use of the traditional function return syntax except in situations that require the trailing return syntax.

Type deduction can’t be used for function parameter types

Many new programmers who learn about type deduction try something like this:

Unfortunately, type deduction doesn’t work for function parameters, and prior to C++20, the above program won’t compile (you’ll get an error about function parameters not being able to have an auto type).

In C++20, the auto keyword was extended so that the above program will compile and function correctly -- however, auto is not invoking type deduction in this case. Rather, it is triggering a different feature called function templates that was designed to actually handle such cases.

Related content

We introduce function templates in lesson 8.13 -- Function templates, and discuss use of auto in the context of function templates in lesson 8.15 -- Function templates with multiple template types.

8.9 -- Introduction to function overloading
8.7 -- Type deduction for objects using the auto keyword

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">