1

After reading this post: Is there a downside to declaring variables with auto in C++? I was asking myself: Is really no one of the answerers aware of auto not beeing a type but a storage-class specifier.

Or is auto since C++11 something different as the storage-class specifier in plain C?

If so, does this break the compability between C and C++?

(I'm aware that they official never were supporting each other in anyway, but my experience was that the C++ comittee tryed to stay as close to C as possible when evver it was acceptable. But now changing an obsolete, but abyway existing keyword instead of just adding a new one. Why here doing such a break of consistens?)

Community
  • 1
  • 1
dhein
  • 6,431
  • 4
  • 42
  • 74
  • 8
    The meaning of the `auto` keyword changed in C++11, it's no longer a storage-class, but is used for type deduction. – Some programmer dude Jan 13 '16 at 09:25
  • 2
    its the first time I hear about `auto` in C, thus I was curious and found this [answer](http://stackoverflow.com/a/2192761/4117728) that basically states, that in C you actually never use the keyword. Thus i guess it is not such a big deal that C++11 changed its meaning. – 463035818_is_not_an_ai Jan 13 '16 at 09:29
  • 1
    It's a completely retarded decision, really. Why couldn't they just come up with a new keyword? – Lundin Jan 13 '16 at 09:30
  • 1
    @Lundin C and C++ are two different languages, so why should they? And new keywords can break existing program so the Standard Committee tries to avoid introducing them whenever possible. Reusing a keyword that was hardy ever used was expedient. – Richard Critten Jan 13 '16 at 09:32
  • 1
    @RichardCritten Because `auto` has a different meaning in C++ prior C++11? It has nothing to do with C. How does _changing the meaning_ of a keyword break the program less than introducing a completely new one? – Lundin Jan 13 '16 at 09:33
  • @Lundin: Adding a new keyword is not easy as it may broke existing code in *unpredictable* way (the new keyword may be already used by user as variable, type, macro, ...), whereas reusing `auto`, it may still break code but in a predictable way. – Jarod42 Jan 13 '16 at 09:36
  • @Lundin Introducing a new keyword could break existing, valid C++ code. Changing the meaning or `auto` is much less likely, easy to detect, and easy to fix. `auto` really had no use in C++ and was deprecated. – juanchopanza Jan 13 '16 at 09:37
  • @tobi303: But it is not invalid if you would use auto. even if I aggree no one actually should. But if some one did in a valid way and then changes to the newer version without noticing, the behavior of the code may silently change, thats what I'm wondering about. see: http://stackoverflow.com/a/34763081/2003898 – dhein Jan 13 '16 at 09:39
  • 2
    But C++11 does not seem the slightest concerned about backwards-compatible anywhere else, so why here? So I think that rationale is complete nonsense. The need for the C++11 `auto` feature to begin with is very questionable. They just like to come up with new features for the sake of doing so. – Lundin Jan 13 '16 at 09:40
  • @tobi303: additional, there are so many keywords reserved by all the multiple isntances of `_` could just take one of these. – dhein Jan 13 '16 at 09:41
  • C++11 was very concerned about backwards compatibility. In practise, "auto" was very, *very*, *VERY* rarely used in C++ code, and any new keyword would be much more likely to conflict with user code. Any other examples of backwards incompatability? – Martin Bonner supports Monica Jan 13 '16 at 09:42
  • @MartinBonner Pick some random C++ feature and search cplusplus.com. You'll likely find numerous different tabs for different versions of the C++ language, because they are non-compatible. [Random example](http://www.cplusplus.com/reference/functional/less/). – Lundin Jan 13 '16 at 09:45
  • New library functions are not backwards-incompatible. Old code continues to work with new libraries. As to your example, artificially constructed code can distinguish the new and old definitions - but real code will continue to work. – Martin Bonner supports Monica Jan 13 '16 at 10:11
  • dont confuse with auto in c and c++ auto in c automatic storage and in c++ it deduces a the value of what type it can be – Kryssel Tillada Jan 13 '16 at 11:02

1 Answers1

9

As of c++11 auto means to infer the type. It was used because adding a new keyword would have caused more c++ programs to break. As a storage specifier, auto is useless because it is the default if no specifier is added.

The only alternative was to follow the approach used in C generics of using a name starting with an underscore. This would have lead to an ugly keyword, that is meant to be regularly used.

user1937198
  • 4,987
  • 4
  • 20
  • 31