C++0x introduces a new keyword, “auto”, for variable declarations which tells the compiler to infer the variable’s type from the assignment expression.
std::list<std::string> foo = getListOfStrings() ; // becomes auto foo = getListOfStrings() ;
While this will save a ton of typing, I think it demonstrates some fundamental issues with the way C++ is being advanced – especially when compared with languages like Python, Erlang and C#.
This handy feature exists in other C-derived languages, in particular C# 4.0 has the same feature but the keyword is different, “var”.
The simple fact is: C++ got it wrong.
Because C++ is a strongly typed language, every variable declaration must begin by specifying the type of value the variable will contain.
From the perspective of writing the compiler specification, where you are specifying the syntax of the language, then “auto” makes complete sense:
[storage specifier(s)] [storage type] [pointer | refererence decorator] name = [statement];
storage specifiers := [const | static | … ]
storage type := [value type | “auto”]
See: the storage type will either be user specified or automatic, ergo auto.
This, unfortunately, has sod all to do with the language as experienced in the field.
auto winningTicketNumber; // No value specified: programmer thinks "automatic value". auto window = new Window(); // User thinks it's an automatic variable, i.e. garbage collected.
I suspect a degree of “not invented here” was involved, as the precedent in several other C and C++ derived languages is “var”
var window = new Window();
The hunch is based on an assortment of C++0x decisions, and this just fits the pattern. I suspect the committee poo-pooed copying things from other languages, but were sold enough on the features to decide to reinvent those particular wheels.
At the same time – they would have had a valid reason not to use “var”, since the languages which use it tend to have garbage collection, which could lead to cross-language confusion.
But “auto” is just terrible.