Thursday, July 15, 2010

C++0x reusing same keywords and symbols for entirely different meanings

One thing especially bad about c++0x is that it seems to give different meanings to already known keywords and symbols.

For example, the array subscript operator "[]", already can be used to define arrays and index them, but now in c++0x they also denote lambda expressions.

But an even more ludicrous example is the keyword "auto".

"auto" in c++0x can be used to infer the type of an identifier, but it just so happens that "auto" already has a meaning in c++.

auto is used to denote "automatic storage", which essentially means stack-storage for variables that weren't declared globally..
So you can do:
auto int i = 0;


The reason you don't ever see code explicitly using auto, is because 'auto' is on by default, so its rarely used.

This means however, that in a c++0x function you should be able to do this:
auto auto i = 0;


I tested this with GCC 4.5.0, and got a compiler error :/
In fact, GCC 4.5.0 with c++0x extensions enabled doesn't even compile "auto int i = 0;"...

I think this is a GCC bug, but still this shows the problems that reusing keywords and symbols can cause.

Also, needless to say, it makes things a lot more confusing for beginners to understand the different meanings of the words...

3 comments:

  1. The use of auto to designate a "storage class" has been removed in C++ 0x.

    ReplyDelete
  2. I see, thanks for the info snogglethorpe.
    Its odd that they removed it, since they usually try to keep as much as possible for backwards compatibility reasons.
    I suppose 'auto' was so rare it doesn't matter; most people probably don't even know about it.

    Another solution would have been of-course to name the c++0x auto keyword something else; C# uses the 'var' keyword instead of auto, but i assume since people like to use 'var' as an identifier instead of a type, it will probably break more code naming it 'var' instead of 'auto'.

    ReplyDelete
  3. this is to avoid people writting a lot in of change in their compiler . actually most of the change affect the standard library not the language it self. as of c++ it has few keyword and should stay so in think.

    ReplyDelete