Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Auto

, 30 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
The new auto keyowrd.

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 it’s 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.

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

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

and:

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.

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.

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

compared to:

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

which I would split to become:

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.

auto ascii_order_function = ascii_order;
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

auto ascii_order_function = ascii_order;

when you look for the “variable” ascii_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)

Share

About the Author

David Corne
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

Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberzssureqh5-Nov-12 14:27 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 30 Oct 2012
Article Copyright 2012 by David Corne
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid