views:

202

answers:

1

In answering this question the question arose as to whether the traditional C meaning of the keyword auto (automatic storage) is still valid in C++0x now that it means type deduction.

I remember that the old meaning of auto should remain where relevant but others disagreed.

auto char c = 42; // either compilation error or c = '*'

Looking at compilers I see the current division.

  1. Old meaning of auto is no longer allowed
    • VS10
    • g++
  2. Old meaning of auto is used where relevant

Do you know which is the correct behaviour?

+14  A: 

No, it is not. In fact, §7.1.6.​4/3 gives the following example:

auto x = 5; // OK: x has type int
const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
static auto y = 0.0; // OK: y has type double
auto int r; // error: auto is not a storage-class-specifier

As you can see, it results in an error. §7.1.6.​5 pretty much seals the deal with:

A program that uses auto in a context not explicitly allowed in this section is ill-formed.

GMan
+1 for telling me something I didn't know. -1 to C++1x committee, for another useless breakage of compatibility with C.
Pavel Radzivilovsky
@Pavel: I'd hardly call it useless or breaking. How often do you find `auto` being used *at all* in current day C or C++ code?
GMan
I see you've initialized 2 auto variables there, I assume it works kinda like a typedef ie. you can't do: `auto x = 5, y = 5.0f;`Will be great for defining begin and end iterators though
identitycrisisuk
GMan: 1. It is definitely breaking of a language, regardless of how many programs it breaks. 2. It is useless, because the same goal could have been achieved without [1]: just retain the functionality of auto as a storage class if type is explicitly specified.
Pavel Radzivilovsky
It breaks no real programs, and it makes the language simpler to understand. The old meaning for auto was useless.
DrPizza
In fact, any program broken deserves to be broken. The old usage was worse than useless; it was misleading.
MSalters
@Pavel: I will accept that theoretically it is a breaking update (because there is, after all, a way to write proper C++03 code that breaks with this change.) but I think you're completely misguided in disagreeing with the change. There should literally be no modern code that uses `auto`, since it's superfluous. Might as well make something useful out of it. Also, your compatibility seems to be with C, which is meaningless and irrelevant. C++ is not C, who cares what doesn't match. Do you complain about templates because they break C compatibility?
GMan
GMan, I don't get it. To break something, even unimportant, one needs at least a tiny wee reason. Such was not supplied. They could introduce this syntax without breaking anything.
Pavel Radzivilovsky
@Pavel: Is "old auto isn't used" not reason enough? No C++ code used `auto`. It literally could be stripped from the language and 99% of code (100% of good code) would work unchanged. Introducing new keywords is far worse then reusing an old used one. I really don't see your complaint. Nothing is broken. If your code breaks from this change, you were writing shitty code, period.
GMan
@GMan I didn't ask for a new keyword. They could use the very same keyword for the new functionality, and still not break anything - easily.
Pavel Radzivilovsky