30
Oct
12

Auto

I don’t think this is widely known but auto was already a keyword in C++ and also C. Prior to it’s 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 lead 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 use 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.

About these ads

0 Responses to “Auto”



  1. Leave a Comment

What did you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 152 other followers

%d bloggers like this: