couple of really noob questions (ranges, toString)
Ali Çehreli
acehreli at yahoo.com
Wed Nov 3 10:54:35 PDT 2010
Jonathan M Davis wrote:
> In some languages (certainly C++, though I'm
> not sure about D), returning a value which isn't used can mean a
wasted copy or
> object construction which can be inefficient (particularly when
dealing with large
> objects on the stack). Whether a pop function returns anything tends
to depend
> on how much an efficiency concern the library writer thinks that it
is and how
> likely they think that it is that the popped value is going to be wanted.
The stronger reason why pop() doesn't return the object in C++ is about
exception safety.
To me this topic is one of the most fascinating stories in C++'s
history. Very briefly, Tom Cargill noted that exception safety gave a
false sense of security and invited the C++ community to write an
"exception-correct version of Stack":
http://ptgmedia.pearsoncmg.com/images/020163371x/supplements/Exception_Handling_Article.html
It took the community years to come up with an understanding of C++
exception safety. Herb Sutter posted the following "guru of the week"
puzzle:
http://www.gotw.ca/gotw/008.htm
which eventually ended up being in his later book "Exceptional C++",
which turned out to be the biggest eye opener for me. Especially the
exception safety section must be understood by any C++ programmer.
The solution for Tom Cargill's challenge turned out to be "cohesion",
where pop() should not return the value; that should be provided by
top(). The efficiency that came from having top() was a bonus.
Ali
More information about the Digitalmars-d-learn
mailing list