Why “auto” is the wrong keyword.

C++0x introduces a new keyword, “auto”, for variable declarations which tells the compiler to infer the variable’s type from the assignment expression.

std::list<std::string> foo = getListOfStrings() ;
// becomes
auto foo = getListOfStrings() ;

While this will save a ton of typing, I think it demonstrates some fundamental issues with the way C++ is being advanced – especially when compared with languages like Python, Erlang and C#.

This handy feature exists in other C-derived languages, in particular C# 4.0 has the same feature but the keyword is different, “var”.

The simple fact is: C++ got it wrong.

Because C++ is a strongly typed language, every variable declaration must begin by specifying the type of value the variable will contain.

From the perspective of writing the compiler specification, where you are specifying the syntax of the language, then “auto” makes complete sense:

[storage specifier(s)] [storage type] [pointer | refererence decorator] name = [statement];
storage specifiers := [const | static | … ]
storage type
:= [value type | “auto”]

See: the storage type will either be user specified or automatic, ergo auto.

This, unfortunately, has sod all to do with the language as experienced in the field.

auto winningTicketNumber;  // No value specified: programmer thinks "automatic value".
auto window = new Window(); // User thinks it's an automatic variable, i.e. garbage collected.

I suspect a degree of “not invented here” was involved, as the precedent in several other C and C++ derived languages is “var”

var window = new Window();

The hunch is based on an assortment of C++0x decisions, and this just fits the pattern. I suspect the committee poo-pooed copying things from other languages, but were sold enough on the features to decide to reinvent those particular wheels.

At the same time – they would have had a valid reason not to use “var”, since the languages which use it tend to have garbage collection, which could lead to cross-language confusion.

But “auto” is just terrible.

Grr.

7 Comments

I have to disagree with you here. “var” sounds like “variable type” to me, which doesn’t make sense in C++ since types are never variable. auto seems logical, maybe “inferred” (inf? ;-) would have been a better choice. I’ve never come across a language that uses “auto” for garbage collected varibles, so my mind is not polluted by whereever you got that… ;-)

I wasn’t championing “var”:

At the same time – they would have had a valid reason not to use “var”, since the languages which use it tend to have garbage collection, which could lead to cross-language confusion.

But “auto” is just too generic and ambiguous…

I do agree with you in regards to auto being strange choice, especially as it’s an already existing keyword. Granted it’s long been useless, but there will be code bases out there using it that have upgrade issues because of it.

If they wanted to drop the old use of auto, they should have deprecated it at the compiler level and used another keyword instead.

Lee Winder :
… especially as it’s an already existing keyword.

Wow, I’ve used C++ for over 15 years and never realized auto was a keyword!

I have to disagree with you here dude. For “auto”, read “automatically deduce type from initializer”. “var” would be worse imo, would be confused with generic variables which can hold anything like in Javascript.

Also this example isn’t possible with “auto”:

auto winningTicketNumber; // No value specified: programmer thinks “automatic value”.

you can’t use auto when just declaring a variable, it must also be initialized.

@ricky
“auto winningTicketNumber” – those were examples of mistakes being made by the programmer, not things you can do.

I wasn’t suggesting var either, esp. read the last full paragraph.

By and large, my problem with “auto” is that it’s just too broad a term, and it’s context sensitive when being read by a human. There are all kinds of approaches that I think would have been better. Even (tounge in check) let winningTicketNumber = getNumber(); … or infer today = todaysDate(); or today? = todaysDate();

Why cant you just change the spelling of the word to be able to use it where it wont cause these problems? Say use a slang for “Auto” use “Ride” Im no programmer just someone who sees things from a common sense perspective :P

Leave a Reply

Name and email address are required. Your email address will not be published.

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

You may use these HTML tags and attributes:

<a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: