Auto

I am sure that I used the auto keyword once in a while during the early days. We used to be concerned with putting things in registers ... that was before optimizing compilers became better at generating code than we assembly language programmers were at writing it from scratch. auto was a holdover from those days, and auto was the implicit storage class of everything not declared static, extern, or register:

int i = 0;      // what you wrote.
auto int i = 0; // what the compiler knew you meant.

I grepped the entire pre-C++11 code base (which on 13 April 2011 is the entire code base) at Digital Gaslight before I wrote this article, and I found not one instance of the token "auto" used as a C++ keyword, so it is not surprising that it could be recycled into a better purpose.

And that better purpose is ...?

auto causes the type of the declared and defined object to be determined ("deduced" is the term used on page 154) by the defining expression on the RHS. The examples given in the standard are trivial:

auto x = 5; // OK: x has type int

But this is hardly useful, nor is it the intention and benefit of this new feature. Instead, take a look at this definition from some of my code (that probably looks like some of your code):

map<double, std::string>::iterator prfm = revFreqMap.begin();

This is an example of the type of thing that is well suited for auto-simplification. Replace the above line with this one:

auto prfm = revFreqMap.begin();

Now, you might ask how the compiler is supposed to resolve the ambiguity involved in knowing which one of these two legal statements should be interpreted to replace the word auto?

map<double, std::string>::iterator prfm = revFreqMap.begin();
map<double, std::string>::const_iterator prfm = revFreqMap.begin();

The easy answer is that there are now functions named cbegin() and cend() that return the const versions of the iterators, and thus eliminate any ambiguity. The more complicated fact is that you if you intended to get the const iterator, and you call begin() rather than cbegin(), you will get a slap on the wrist telling you that your non-const iterator will not work on a const object.