Search

4.8 — The auto keyword

Auto prior to C++11

Prior to C++11, the auto keyword was probably the least used keyword in C++. In lesson 4.1a -- Local variables and local scope, you learned that local variables have automatic duration (they’re created at the point of definition, and destroyed when the block they are part of is exited).

The auto keyword was a way to explicitly specify that a variable should have automatic duration:

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

Automatic type deduction in C++11

In C++11, the meaning of the auto keyword has changed, and it is now a useful addition to your C++ vocabulary.

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 assigning to it?

Starting with C++11, the auto keyword does just that. When initializing a variable, the auto keyword can be used in place of the variable type to tell the compiler to infer the variable’s type from the assignment’s type. This is called automatic type deduction.

For example:

This even works with the return values from functions:

Note that this only works when initializing a variable upon creation. Variables created without initialization values can not use this feature (as C++ has no context from which to deduce the type).

Note that as of C++14, with one exception (lambda expressions), auto can’t be used as a type for function parameters. If you’re looking to create functions that work with a variety of different types, you should be using function templates, not automatic type deduction. This restriction may be lifted in future versions of C++ (with auto acting as a shorthand way to create function templates).

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 be very nice.

Automatic type deduction for functions in C++14

In C++14, the auto keyword was extended to be able to auto-deduce a function’s return type. Consider:

Since x + y evaluates to an integer, the compiler will deduce this function should have a return type of int.

While this may seem neat, we recommend that this syntax be avoided for 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.

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 right there, 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 in C++11

C++11 also added the ability to use a trailing return syntax, where the return type is specified after the rest of the function prototype.

Consider the following function declaration:

In C++11, this could be equivalently written as:

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

But it is of more use when combined with some advanced C++ features, such as classes and the decltype keyword. We’ll talk more about the other auto uses when we cover the decltype keyword.

For now, we recommend the continued use of the traditional function return syntax.

Summary

Starting with C++11, the auto keyword can be used in place of a variable’s type when doing an initialization in order to perform automatic type deduction.

Other uses of the auto keyword should generally be avoided except on an as-needed basis.

4.x -- Chapter 4 comprehensive quiz
Index
4.7 -- Structs

22 comments to 4.8 — The auto keyword

  • Boyan

    Just above the "Automatic type deduction for functions in C++14" headline, you’ve written "we’ll we will see "

    I’ve got to say though…this is probably the best tutorial I’ve seen on ANYTHING, let alone C++! Thank you for the awesome work! 🙂

  • Yup Boyan is right. Remove "we will" from the following sentence:
    "While using auto in place of fundamental data types only saves a few (if any) keystrokes, in future lessons we’ll we will see examples where the types get complex and lengthy."
    Just highlighting the typo made in this section. 🙂

  • programmer, another one

    hello alex,

    I used the auto keyword in a class as a public member on two variables in Qt creator, and the compiler flagged it as an error saying that: "a non-static data member cannot have a type that contains auto".

    do you know why this may have occurred?

    thanks

  • hridayesh

    hey alex i am using codeblocks and this program is throwing error saying that k and l are not declared.

  • Sandro

    "auto d = 5.0; // 5.0 is a double literal, so d will be type double"

    It’s a double literal because there is no "f" or "l" at the end of the number?

  • Ola Sh

    Thanks for the good work. In my opinion, this is the best programming tutorial that I have used. Thanks again.

  • shreyanshd

    Would the use of automatic type deduction in C++ affect the performance?  If auto keyword is used extensively in C++ then the compiler has to figure out the data type every time, which would degrade performance.

    Correct me if I am wrong.
    Great tutorials. Love them 🙂

    • Alex

      No, the compiler already needs to know the type in order to do strong type checking (to give you an error if you try to do something nonsensical). Auto is really just a convenience for the programmer.

  • Dekasi

    "While this may seem neat, we recommend that this syntax be avoided for functions that return a fixed type. The return type of a function is of great use in helping to document a function. When a specific type isn’t specified, the user may not know what is expected.

    (Side note: 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 within the boundaries of a statement, because the type being inferred often doesn’t matter, and if it does, the actual type information is generally at hand. However, across statement boundaries, it’s better for documentation and maintenance to make types explicit.)"

    I don’t understand few things in this excerpt:
    1. what are fixed types
    2. to document a function
    3. in which case you think that the user needs to know to type
    4. what can be referred as a boundary of statement
    5. `the type being inferred often doesn’t matter, and if it does, the actual type information is generally at hand`- what you mean with this

    • Alex

      Rather than answer all of these questions, I updated the wording of that section substantially. Have another read and see if it’s clearer now. If not, let me know what’s still confusing.

  • Matt

    In the first paragraph, the wording needs to be updated to say that local variables are created upon definition(not when entering the block).
    Hope I’m not getting annoying 🙂

  • Matt

    Can the auto keyword be used for function return type deduction when the return type is void(or as part of the trailing return type syntax)?

  • selami

    auto keyword can easily be used by int, double, char etc.. but not used with string.
    why? when used. it is written as PKc. what does it mean?

  • Zachary Fojtasek

    Why can I not do something like this?:

    this does not compile.

    But if it did, I could use a function like this instead of overloading functions.

    • Alex

      Auto tells the compiler to infer the proper type -- because the compiler does the inferring, the type has to be inferable at compile-time. Your example doesn’t work because x and y (and the thus the return type) can’t be determined at compile time.

      However, C++ does provide functionality to do what you want. In chapter 13 I talk about function templates, which are designed to do exactly what you’re intending here.

      (Note that this restriction may be lifted in future versions of C++)

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter