Different types with auto
bearophile
bearophileHUGS at lycos.com
Thu Mar 17 09:45:15 PDT 2011
I have found this article through Reddit (the Reddit page already contains comments by Andrei), "C++0x feature support in GCC 4.5" by Arpan Sen:
http://www.ibm.com/developerworks/aix/library/au-gcc/index.html
The article probably doesn't contain new things for people that have closely followed the design and development of C++0x, but for less experienced people it shows several D features that are now more or less present in C++ too (as soon as C++ compilers catch up).
One thing of that page has caught my attention, in Listing 7, about erroneous use of the auto keyword:
int main( )
{
auto i = 9, j = 8.2; // error i and j should be same type
auto k = &k; // dumb error cant declare and use in initializer
...
}
According to the article the first line generates the error:
1.cpp:3:8: error: inconsistent deduction for 'auto': 'int' and then 'double'
This is a difference between D auto and C++0x auto, because this is accepoted by D:
void main() {
auto i = 9, j = 8.2;
}
There was a thread about related matters in the d.learn newsgroup, started by Ellery Newcomer:
http://www.mail-archive.com/digitalmars-d-learn@puremagic.com/msg09361.html
D disallows bug-prone C syntax like this (C style guides strongly suggest to declare only each variable in a distinct statement and line of code):
int a = 1, *b = null;
D accepts code like:
> auto a = 1, b = null;
This seems against the D rule of not allowing different types to be initialized in the same statement. In my opinion on this design detail D is worse than C++0x. As an example, if you write a line of code like this, meaning it to initialize six double variables, you have a bug:
auto x1=1., x2=2., x3=3., x4=4., x5=5, x6=6.;
-------------------------
There are only few (one?) other thing(s) that I think C++0x gets better than D, like a more strict enum, I don't understand why D doesn't follows C++0x design on this other detail:
http://d.puremagic.com/issues/show_bug.cgi?id=3999
Bye,
bearophile
More information about the Digitalmars-d
mailing list