Re: An interesting blog




"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. If you want the object created in that case, but not for assignment, why?

Just use find and de-reference the iterator. the de-reference of the iterator when it's m.end() may indeed throw an exception
It's a shortcut for insert, except that it provides immediate access to the mapped item.

If you don't like the semantics of std::map[] operator......don't....use.....it.

Dennis

.



Relevant Pages

  • Re: Printers question
    ... an IP address that's unique to it (ie not assigned by DHCP) and then ... I did to get the thing setup and working okay. ... I discovered from direct experience how uppity initial HP network printing ... setupconfig and changed from DHCP ip address assignment to ...
    (comp.sys.acorn.apps)
  • Re: execute "jobs" in my shell
    ... Okay, I got it. ... It's the assignment description's fault. ... scheduling build-in commands. ...
    (comp.unix.shell)
  • Re: Tuples
    ... Tuvas wrote: ... Okay, everything works here as expected except the last line. ... And the reason is explicitly stated (tuples don't support item assignment) ...
    (comp.lang.python)
  • Re: selection-sort in C
    ... Okay, today my confusion of parameter vs argument is cleared. ... full initialisation of whole object. ... think this is from where my initialization vs assignment idea came from. ...
    (comp.lang.c)
  • Re: Nested iterators (well, not nested exactly...)
    ... > Russ Perry Jr wrote: ... > via the corresponding Iterator. ... Okay, cool. ... the same one (I suppose it would be a class method rather than an ...
    (comp.lang.java.programmer)