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:
auto i = 0; auto message = "hey guys";
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; return 0;
}
and:
int main() {
auto i = 0; 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;
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) {
}
compared to:
for (std::list<T>::iterator iter = sorted.begin(); iter != sorted.end(); ++iter) {
}
which I would split to become:
for (
std::list<T>::iterator iter = sorted.begin();
iter != sorted.end();
++iter
) {
}
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.