Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Auto

4.80/5 (3 votes)
30 Oct 2012CPOL2 min read 6K  
The new auto keyword

I don’t think this is widely known but auto was already a keyword in C++ and also C. Prior to its removal in C++09, it was used as a storage duration indicator. What this means is that it tells the compiler how long the storage for the variable needs to last for. The auto keyword is the default and means that the storage is available until the end of the block in which the variable is created. The fact that this is the default behaviour for C++ and its infrequent use led to it being removed from the standard. As Bjarne Stroustrup writes:

Several committee members trawled through millions of lines of code finding only a handful of uses — and most of those were in test suites or appeared to be bugs.

So onto the new auto. It’s used now is to implicitly set the type of a variable. This is demonstrated in the following code:

C++
auto i = 0;                // an integer
auto message = "hey guys"; // a const char*

When To Use It, And When To Not Use It

In a statically typed language, the question of where to use auto is decided by whether the code is more readable or appealing to use. This is because the type is found at compile time at little cost and the produced executable is the same. I verified this with an experiment generating assembly for:

C++
int main() {
  int i = 0; // an integer
  return 0;
}

and:

C++
int main() {
  auto i = 0; // an integer
  return 0;
}

then diffing the two results, which were the same.

So the question remains where is it more readable to use auto? I think that with all new features of a language, people (probably myself included) will go overboard and try to use them in as many places as they can. However, auto can obfuscate code for example.

C++
auto pi = 3.14159265359; // a float or a double?

I do feel that there are places where it makes code more readable for instance in standard iterators.

C++
for (auto iter = sorted.begin(); iter != sorted.end(); ++iter) {
  // code body here
}

compared to:

C++
for (std::list<T>::iterator iter = sorted.begin(); iter != sorted.end(); ++iter) {
  // code body here
}

which I would split to become:

C++
for (
  std::list<T>::iterator iter = sorted.begin();
  iter != sorted.end(); 
  ++iter
) {
  // code body here
}

Personally, I think that the first is the most readable.

Another case where it works very nicely is with function pointers. Compare the following code:

C++
auto ascii_order_function = ascii_order;
C++
bool (*ascii_order_function)(const string& first, const string& second) 
    = ascii_order;

Code for the first example can be found in this repository if you want to see the context and functions used.

This is the best use I have found for auto as anything which reduces the horrible syntax of function pointers is good in my book. Although it is mysterious looking at the line...

C++
auto ascii_order_function = ascii_order;

...when you look for the “variableascii_order and find it is a function, I feel it becomes unambiguous.

I don’t think that there are many places where auto can be used well. As it is a shiny new feature, I’m sure it will be overused. Before using auto ask, does this make my code more readable? Will someone maintaining this code know by looking at the variables on that line know what type it is?

Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)