Re: An interesting blog




"jacob navia" <jacob@xxxxxxxxxxxx> wrote in message news:i2hsdk$djo$1@xxxxxxxxxxxxxxxxxxxx
Dennis (Icarus) a écrit :

"jacob navia" <jacob@xxxxxxxxxxxx> wrote in message news:i2fm7j$vet$1@xxxxxxxxxxxxxxxxxxxx
http://apenwarr.ca/log/?m=201007#22

<quote>

Okay, one more example of C++ terribleness. This one is actually a tricky one, so I can almost forgive the C++ guys for not thinking up the "right" solution. But it came up again for me the other day, so I'll rant about it too: dictionary item assignment.

What happens when you have, say, a std::map of std::string and you do m[5] = "chicken"? Moreover, what happens if there is no m[5] and you do std::string x = m[5]?

Answer: m[5] "autovivifies" a new, empty string and stores it in location 5. Then it returns a reference to that location, which in the first example, you reassign using std::string::operator=. In the second example, the autovivified string is copied to x - and left happily floating around, empty, in m[5].

Ha ha! In what universe are these semantics reasonable? In what rational

Mine, and likely several others :-)

set of rules does the right-hand-side of an assignment statement get modified by default? Maybe I'm crazy - no, that's not it - but when I write m[5] and there's no m[5], I think there are only two things that are okay to happen. Either m[5] returns NULL (a passive indicator that there is no m[5], like you'd expect from C) or m[5] throws an exception (an aggressive indicator that there is no m[5], like you'd see in python).


How about foo(m[5]) Same thing right? m[5] is being assigned/copied/rererenced to the first argument of foo. Thart won't be helped by defining a new operator.

Look, he wrote:

when I write m[5] and there's no m[5], I think there are only two things that are okay to happen.

Either m[5] returns NULL (a passive indicator that there is no m[5], like you'd expect from C) or m[5] throws an exception (an aggressive indicator that there is no m[5], like you'd see in python).

My point was that
m[5] = some_value;
is no different than a function that'd write to m[5] as an output.

Dennis

.