6.13 — The auto keyword

Prior to C++11, the auto keyword was probably the least used keyword in C++. However, C++11 and newer language standards have given new life to the auto keyword. We’ll cover many of those uses here.

Consider the following statement:

If C++ already knows 5.0 is a double literal, why do we have to explicitly specify that d is actually a double? Wouldn’t it be nice if we could tell a variable to just assume the proper type based on the value we’re initializing it with?

Type inference for initialized variables

When initializing a variable, the auto keyword can be used in place of the type to tell the compiler to infer the variable’s type from the initializer’s type. This is called type inference (also sometimes called type deduction).

For example:

This also works with the return values from functions:

While using auto in place of fundamental data types only saves a few (if any) keystrokes, in future lessons we will see examples where the types get complex and lengthy. In those cases, using auto can save a lot of typing.

Type inference for functions in C++14

In C++14, the auto keyword was extended to be able to deduce a function’s return type from return statements in the function body. Consider the following program:

Since x + y evaluates to an int, the compiler will deduce this function should have a return type of int. When using an auto return type, all return statements must return the same type, otherwise an error will result.

While this may seem neat, we recommend that this syntax be avoided for normal functions. The return type of a function is of great use in helping to document for the caller what a function is expected to return. When a specific type isn’t specified, the caller may misinterpret what type the function will return, which can lead to inadvertent errors.

Best practice

Avoid using type inference for function return types.

Interested readers may wonder why using auto when initializing variables is okay, but not recommended for function return types. A good rule of thumb is that auto is okay to use when defining a variable, because the object the variable is inferring a type from is visible on the right side of the statement. However, with functions, that is not the case -- there’s no context to help indicate what type the function returns. A user would actually have to dig into the function body itself to determine what type the function returned. It’s much less intuitive, and therefore more error prone.

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 declaration:

Using auto, this could be equivalently written as:

In this case, auto does not perform type inference -- 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:

For now, we recommend the continued use of the traditional function return syntax. But we’ll see this trailing return type syntax crop up again in a future lesson when we cover lambda expressions.

Type inference for function parameter types

Many new programmers try something like this:

Prior to C++20, this won’t work, because the compiler can’t infer types for function parameters x and y at compile time. Pre-C++20, if you’re looking to create generic functions that work with a variety of different types, you should be using function templates (covered in a later chapter), not type inference.

Starting in C++20, the auto keyword can be used as a shorthand way to create function templates, so the above code will compile and run. Note that this use of auto does not perform type inference.

For advanced readers

Lambda expressions have supported auto parameters since C++14. We’ll cover lambda expressions in a future lesson.

As an aside...

If you’re wondering what the auto keyword did pre-C++11, it was used to explicitly specify that a variable should have automatic duration:

However, since all local variables in modern C++ default to automatic duration unless otherwise specified, the auto keyword was superfluous, and thus obsolete.

6.14 -- Implicit type conversion (coercion)
6.12 -- Typedefs and type aliases

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="">