5

Theoretical question only - why i can't write such code:

auto auto foo = 0;

First auto keyword - storage class specifier (yeah, i know that it's useless and deprecated in C++11), second auto keyword - auto type-specifier.

So what's wrong?

And again - i don't really want to use this in real code.

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • Take a look at [dcl.spec.auto] in the C++11 standard. *5: A program that uses auto in a context not explicitly allowed in this section is ill-formed.* There's no mention before that of the C++03 usage. – chris Aug 29 '12 at 17:55
  • They really removed old meaning of `auto`. See my own question: http://stackoverflow.com/questions/12093717/why-old-usage-c03-of-auto-does-not-compile-under-c11. Maybe not related - but I believe this is perfectly valid: `static auto a = 7;` – PiotrNycz Aug 29 '12 at 18:44

2 Answers2

20

The auto storage class specifier is not "useless and deprecated in C++11," it has been removed entirely. The auto keyword is no longer a storage class specifier and cannot be used as one.

In C++11, auto is a simple type specifier.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Oh, i see: Change: Remove auto as a storage class specifier – FrozenHeart Aug 29 '12 at 17:56
  • 2
    Yes, it is a breaking change, but it doesn't matter because the old use of `auto` (as a storage class specifier) was superfluous. – James McNellis Aug 29 '12 at 17:59
  • 1
    Also important: it's a noisy breaking change, since old broken code won't compile under the new language rules. Silent breaking changes are far worse, though C++11 has [some of those too](http://stackoverflow.com/questions/5759232/stdvector-default-construction-c11-and-breaking-changes). – James McNellis Aug 29 '12 at 18:05
3

From the Stroustrup's FAQ:

....The old meaning of auto ("this is a local variable") is now illegal. 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.

Which indicates there's much not code used using "auto" as storage specifier.

P.P
  • 117,907
  • 20
  • 175
  • 238