Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

The Last Word on auto (For Now)

August 17, 2011

The discussion about auto has meandered about a bit (see previous posts here and here), so I'd like to point out what I think are the important themes before moving on to other topics.

When I write

auto var = expression;

I am saying that I want var to have the same type as expression. The main objection that readers have raised to this technique is that we generally can't tell by looking at the code what type var has. This lack of knowledge makes programs hard to understand, so programmers should say explicitly what the type is. I have responded to that objection in several ways:

1. If you say explicitly what type var has, you now know its type; but you no longer know that var has the same type as the expression. So, for example, if I write

int N = 100000;

I know that N has type int, but I don't know that N has the same type as 100000. Even in this seemingly simple example, this lack of knowledge can lead to serious trouble. For example, I might be running on an implementation in which int cannot contain the value 100000, so that 100000 has type long. If I had written

auto N = 100000;

then I would have been assured that N is indeed 100000; but stating the type as int would yield undefined behavior.

2. auto is far from the only context in which you don't know what type a variable has when you write your code. For example:

template<class T> void f(T t) { /* … */ }

Here, t could have just about any type; there's no way to tell until you compile the program whether that type makes sense in its context.

3. Not only do you not always know what types you're using, but it can be hard to figure out how to write those types. For example, if c is a container, I might write

auto b = c.begin();

If I'm not supposed to use auto, how do I figure out what type to define for b? If I knew that c had type C, I could write

C::iterator b = c.begin();

However, this technique works only because containers follow the convention of having type members named iterator that describe the appropriate iterator types. If we are dealing with a user-defined container-like class that does not follow these conventions, we would have to write something like

decltype(c.begin()) b = c.begin();

which, as far as I am concerned, has no advantage at all over using auto.

In other words, there are many circumstances in which it makes sense to use auto — even if we ignore the question of readability. But readability is a slippery concept. People are excellent pattern matchers, and when they've seen particular patterns in use for a while, they become familiar with them. Until then, those patterns can be hard to understand. For example:

         *p++ = *q++;

If you are at all serious about programming in C or C++, then at some point you spent some time figuring out what this statement could possibly mean and how it works. Later, this usage became familiar, and you no longer had any trouble understanding it. More generally, I think that when people say that a piece of code is hard to understand, what they really mean is that they don't understand it. This lack of understanding could come about for many reasons, not all of which have to do entirely with the code. In particular, the code may become easier to understand with time.

For these reasons, I think that programmers should approach auto in the same way that they approach other language features or programming techniques, namely by asking whether auto is the most straightforward way of solving the problem at hand. If it's unfamiliar, one may have to think about its meaning compared to its alternatives; but if the meaning makes sense, I think it is better to become familiar with it than to reject it on the basis of unfamiliarity alone.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.