Jon Stewart on 24 Nov 2003 23:30:22 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] How Perl sucks even worse than C++ when it comes to assoc arrays


> So, more than a few times at meetings we've talked about how map's can
> be used as assoc arrays, and how evil the implementation is. In
> particular, in order to allow adding any entry by:
> 
> myMap[myKey] = myValue;
> 
> in the case where there was no prior entry for myKey, operator[] is a
> non-const method which will actually add an entry to the map if myKey is
> not already in there.


As discussed before, this behavior is particularly insidious when you want 
to index into the map for an r-value:

if(myValue == myMap[myKey])

No assignment in sight, but if myKey doesn't exist in the map, it will be 
created with a default-constructed value (instead of throwing an exception 
or returning a dummy value that you could test, etc.).

If you are using such an expression with a const map, the compiler will 
bitch. Thus it pays to be fussy with const.


> So, the next time you think that C++'s approach to the problem is
> dangerous and error prone, take a close look at Perl. ;-)


Heh. Perl.



Jon

-- 
Jon Stewart                                 Advanced Los Angeles C++
stew1@xxxxxxxxxxx                           http://www.alacpp.org
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp