Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C++

Auto

Rate me:
Please Sign up or sign in to vote.
4.80/5 (3 votes)
30 Oct 2012CPOL2 min read 5.8K   4   1
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.

This article was originally posted at http://davidcorne.com/2012/10/30/auto

License

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


Written By
Software Developer
United Kingdom United Kingdom
I am a C++ developer with a strong interest in Python, C#, and Qt. I work on a native C++ application which uses COM to call C# in order to use a WPF GUI.

While I develop an application using WPF exclusivly for windows, I am a big linux user. My favourite distro at the moment is Linux Mint, and I love the delights of the command line,.

If you've read something of mine and you enjoyed it, check out my blog.

I am also active on various other sites, listed below.

Coding Sites



  • BitBucket where I keep the majority of my projects.
  • GitHub where I have a few older projects. This includes my current long term project, I'm writing a book about design patterns in python. Find the repository here and blog posts about individual patterns here
  • Stackoverflow I'm not too active on stackoverflow, I'm more of a listener.
  • coderwall and coderbits two profile compilation websites.


Social Sites



Comments and Discussions

 
GeneralMy vote of 4 Pin
zssure5-Nov-12 13:27
zssure5-Nov-12 13:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.