I was recently wondering why the keyword auto was chosen in C++11 to mark a variable whose type must be inferred by the compiler, like in

auto x = 1;


  1. var seems more common in other programming languages (e.g. C#, Scala, JavaScript), and
  2. As far as I understand the new semantics of auto breaks backward compatibility (it was rarely used but had a different meaning in previous revisions of C++, see e.g. here)

I wanted to ask if there was a special reason for choosing auto (in favour of var or any other keyword). Was there any specific discussion about this issue before the C++11 standard was released?

Also, are there any possible incompatibilities we should watch out for when recompiling legacy C++ code with a C++11 compiler?

  • 9
    The new semantics of auto might break backwards compatibility, but, depending on how often var is used as a variable name compared to how often the auto keyword is used in pre-11 code, the committee might have opined that it breaks compatibility less dramatically than introducing a new keyword would.
    – sepp2k
    Dec 27 '12 at 14:39
  • 2
    "As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.": This question is asking about a fact: was there a discussion about this topic. There are two possible answers: YES and NO.
    – Giorgio
    Dec 27 '12 at 14:47
  • 2
    Of course there was a discussion, which makes the question kinda pointless in that regard. The auto vs var question is what 90% of your text refers to, and that question has no definitive result. (though I'm not the one who has voted to close)
    – Telastyn
    Dec 27 '12 at 14:52
  • 1
    @Telastyn: If I had known that there had been a discussion on this topic I would not have asked. Googling for "auto versus var C++" or "auto C++" returned nothing on this topic.
    – Giorgio
    Dec 27 '12 at 15:00
  • 1
    auto was proposed for C++ before var was introduced in C# so the question should be why C# does not use auto. var has a different meaning in JavaScript and Scala
    – adrianm
    Dec 28 '12 at 17:22

Almost every word you might think of adding as a keyword to a language has almost certainly been used as a variable name or some other part of working code. This code would be broken if you made that word a keyword.

The incredibly lucky thing about auto is that it already was a keyword, so people didn't have variables with that name, but nobody used it, because it was the default. Why type:

auto int i=0;


int i=0;

meant exactly the same thing?

I suppose somewhere on the planet there was some small amount of code that used 'auto' the old way. But it could be fixed by removing the 'auto' and it would be working again. So it was a pretty obvious choice to repurpose the keyword.

I also happen to think it's a clearer meaning. If you've worked with variants and such, when you see var you may think that the declaration is somehow less strongly typed than if you pressed all the keys yourself on the keyboard to specify the type of the variable. To me, auto makes it clearer that you're asking the compiler to automatically deduce the type, which is just as strong as if you had specified it yourself. So it really was a very lucky break that made a good name available to the committee.

To clarify the (small) breaking:

If you had

auto int i=0;

and tried to compile with a C++ 11 compiler, you will now get an error such as

error C3530: 'auto' cannot be combined with any other type-specifier

This is trivial, you just remove either the auto or the int and recompile.

There is a bigger problem though. If you had

auto i = 4.3;

C and the really old C++ would make i an int (as it would if you left off auto - default declaration was int). If you have gone a really long time without compiling this code, or have been using old compilers, you could have some of this code, at least in theory. C++ 11 would make it a double since that's what 4.3 is. (Or maybe a float, I'm still in Boxing Day mode, but the point is, not an int.) This might introduce subtle bugs throughout your app. And without any warnings or errors from the compiler. People in this boat should search globablly for auto to make sure they weren't using it the old way before they move to a C++ 11 compiler. Luckily, such code is extremely rare.

  • Thanks for a very clear answer. +1 I understand the trade-off between dropping backward compatibility in a mostly harmless way and having a big chance that the old code is not broken by the new keyword.
    – Giorgio
    Dec 27 '12 at 14:56
  • Is there actually any incompatibility at all? Won't a C++11 compiler just ignore the auto if it is followed by a type name? Dec 27 '12 at 15:28
  • 1
    Visual C++ 2012 says error C3530: 'auto' cannot be combined with any other type-specifier to that line Dec 27 '12 at 15:34
  • 3
    @KateGregory: Actually, there is no problem with auto i = 4.3;, because that was ill-formed in C++03/C++98. C++ did not carry over the 'implicit int' rule that C89 had (and dropped in the C99 revision). Dec 27 '12 at 16:16
  • 1
    @Kate Gregory: If you could take Bart's observations into account and change your answer accordingly I would mark it as the accepted answer.
    – Giorgio
    Dec 28 '12 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.