6.14 — The auto keyword

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

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 lesson 7.15 -- Introduction to lambdas (anonymous functions).

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.

6.15 -- Implicit type conversion (coercion)
6.13 -- Typedefs and type aliases

67 comments to 6.14 — The auto keyword

  • sami


    According to the following, the code below worked, am I was wrong on understanding the concept below?

    "When using an auto return type, all return statements must return the same type, otherwise an error will result."

    • nascardriver

      I don't understand your question. `myFunc` has a single return-statement, so all return-statements in that function return the same type.

    • Somto

      I think you're mixing up return types with parameter types. Even if you changed the type of "y" you still have one return statement "return x + y"

  • Juan

    Would auto work for trying to take input values from a user?

    Say I want to make a function that divides two numbers the user inputs, but I have no way of knowing if they will input an int or a double. Ideally if the user inputs two int values, then I'd like them to behave accordingly to avoid any sort of approximations. Likewise, if the user inputs double values, then ideally they'd behave like doubles.

    I just don't know how I would go about writing something like this, as I need to have already initialized a variable to even be able to input a new value into it, and by that point it will already be decided whether I'm using int or double. Is it possible to do this at all?

    • nascardriver

      All types have to be known at compile-time.
      You can do what you're trying to do by reading the user input into a string, then examining the string to figure out which type you need. Still, you need to have written code for every type you want to handle.

  • cgsousa

    After I got to know this tutorial, I continued to study C ++, congratulations on the work.
    Just a question about "Type inference for function parameter types"

    this feature of passing an unknown type as an argument, would it be the "Variant"? Also known in other languages, like object-pascal ?

    • nascardriver

      Variants are different, but similar. I don't know pascal, but at least in C++ that's the case. Like `auto`, `std::variant` can have different types. Though, you have to specify a list of types that it can potentially hold, and it can change types at any point. Unlike `auto` variables, which cannot change types, their type is deduced at compile-time.

      What you're getting at are templates. We talk about templates later.

  • koe

    It might be good to explicitly say at the beginning of the section that type inference with 'auto' was added in C++11.

    UPDATE: It looks like trailing return type syntax works for C++11, and in the function definition (not just function prototypes). Might be good to update.

    • nascardriver

      We assume C++11 to be the absolute minimum. Notes about C++11 (eg. feature xyz was added in C++11) are in the process of being removed. I removed the notes from this lesson as well.
      I modified the examples of the trailing return type to be definitions rather than only declarations. Now there are trailing auto return type examples for declarations and definitions. Thanks for pointing this out :)

  • Henry

    So basically, c++ keyword auto has the same purpose as  c#, javaScripts etc keyword var?

  • Fernando


    1) Is there any performance-wise drawback for using "auto"?

    2) You mentioned that the compiler might guess your variable type wrong (not using these words though, and correct me if I'm wrong). Does this mean that duck-typed programming languages like Python suffer from this drawback by design?


    • Alex

      1) No.
      2) The compiler won't guess it wrong. However, if you use auto as a function return value, the caller of the function might get it wrong. This could cause any number of problems to manifest, possibly resulting in incorrect or undefined behavior.

  • Edwin Martens

    auto is evil !
    why ?

    auto value = doSomethingWith(int x);

    The compiler may see the type of value, BUT YOU DO NOT !!!

    what type is value here ?, anyone ?

    • Alex

      Auto isn't evil, it just needs to be used appropriately.

      Your example would be a misuse, as it gives no context as to what type of return value to expect. But something like:

      is fine -- it's clear we're returning some kind of Dictionary object.

      A good rule of thumb is: "Use auto when it's hard to write the type, but the type is obvious".

  • Anthony

    I've been using the auto keyword quite freely. But I can't figure out what the actual type is in this example:

    • I can't reproduce your error. Please post a minimal example that causes the error.

      • Anthony

        Not sure what you mean. Should I post a minimal compilable version?

        Btw, when I hover over 'begin' in both MSVC AND VSCode, it says 'container<char>::iterator' and I'm very surprised this isn't correct.

        • It is `container<char>::iterator`. There's something else wrong in your code.

          > Should I post a minimal compilable version?
          "compilable" is going to be hard, because your code doesn't compile. Post a minimal version that causes your error and no other errors.

          • Anthony


            Finally returned to this problem. As previously stated, @auto works, but when I attempt to do it 'manually', I get an error. Here's code showing the problem:


    When will you start advance c++ topics. I really need it.

  • Perhaps I'm a bit early, but I am really wondering if auto makes my life easier.

    I do see the sense in Go where auto is not needed

    I could see the sense if something like this were possible:

    Well, I guess not... but maybe I am just getting ahead of things.

    (BTW. Just to note... This is the 6th C++ tutorial I've seen, but the first one in which a simple "Hello World" example didn't give me compile errors, already, and this tutorial made me write the first C++ code that actually WORKS. I always felt silly that I've been coding for almost 35 years and I could never master C and C++, so thanks a lot). ;)

    • Snippet 1
      * Line 1, 2, 4: Initialize your variables with brace initializers. You used copy initialization.

      @auto comes in handy when you get to templates. Types can fill up entire lines

  • Gurdeep Singh

    [auto calculateThis(int x, double d) -> std::string;] How it evauluates to std::string. Should'nt it be double?

    • Hi!

      Without knowing what @calculateThis does, it could return anything. Judging by the function's name, you're right, it should most likely return an int or double.

      • Alex

        The trailing return type syntax moves the return type from before the function name to after the function name.

        "auto calculateThis(int x, double d) -> std::string" clearly returns a std::string (as the "-> std::string" part of the declaration indicates).

        auto in this context doesn't perform type inference in this context, it just acts as a placeholder.

  • magaji::hussaini

    I am not in love with this "auto" keyword to be used for variables either, makes me kinda lazy not to even care about my variable type just like dynamically typed languages cant even use unsigned types, and I think it increases compilation time also

  • Pabitra Padhy

    Hey Alex,

    Although Type Inference could be applied to members of struct or class
    we have to make them static and const, then we could initialize them.

    I don't see any use for this.
    Could you care to explain, what could be the uses for such an use of Type Inference ?

  • Blaqsmite

    hi Alex
    Am I doing something wrong or is ok to say that auto cant be used to define a variable in a .h file?
    I am attempting to do this in visual studio 2017 which i am guessing uses vc2015. I dont what version of Cpp it uses.
    great tutorials by the way

    • nascardriver

      Hi Balqsmite!

      auto can be used in header files just fine, without code I'm guessing you're not initializing the variable in which case there's no way for your compiler to know which data type it should be.

  • himanshu shivnani

    The article is very useful Alex. Keep it up!!

  • vd

    Precise and well written. Thank you for this tutorial!

  • antiriad7

    Why did u write &x and &y and not x and y in function parameters?

  • Peter

    > This is called automatic type deduction.

    Actually AFAIK it's simply called type inference -

  • 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++)

  • 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?

  • 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)?

  • 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 :)

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

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

  • Ola Sh

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

  • 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?

  • hridayesh

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

  • 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?


  • 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. :)

  • 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! :)

Leave a Comment

Put all code inside code tags: [code]your code here[/code]