Proposal: takeFront and takeBack

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 4 13:26:36 PDT 2012


On Wednesday, July 04, 2012 22:10:36 Tobias Pankrath wrote:
> On Wednesday, 4 July 2012 at 18:40:50 UTC, Jonathan M Davis wrote:
> > On Wednesday, July 04, 2012 12:55:44 Tobias Pankrath wrote:
> >> > Many languages does this (it doesn't mean it is the right
> >> > thing
> >> > to do). Do you know why this shouldn't be done ?
> >> 
> >> In C++ it was exception safety, wasn't it?
> > 
> > I believe that it was purely a question of speed. If popFront
> > returns an
> > element, then that element gets copied, and if you didn't need
> > to access the
> > element, then that's wasted cycles. You have to worry about
> > exceptions in
> > either case, depending on the what popFront is doing.
> > 
> > - Jonathan M Davis
> 
> If you pop from a container and return this the copy constructor
> / postblit will run. If in this moment a exception is thrown,
> than this value is lost.

Yes. But the cost of copying the value and the cost of the exception are 
separate. Simply popping off the element could throw an exception (in the case 
of strings in D, you'll get a UTFException if the string is malformed). So, 
_regardless_ of whether you return an element, you potentially risk an 
exception being thrown (depending on the implementation of the container or 
range). And so I think that exceptions are pretty much irrelevant to the 
discussion of whether returning an element from popFront is a good idea or 
not.

- Jonathan M Davis


More information about the Digitalmars-d mailing list