Safer casts
Sean Kelly
sean at invisibleduck.org
Mon May 12 18:11:43 PDT 2008
== Quote from Dee Girl (deegirl at noreply.com)'s article
> Yigal Chripun Wrote:
> > Dee Girl wrote:
> > <snip>
> > maybe I still didn't explain myself properly. I've used the term
> > "sort-with-delegates" meaning a specific instance of sort when given a
> > delegate as input. I thought this was clear enough, otherwise I would
> > have simply said "sort".
> > before you start sending me assembly code, let's go over it again and
> > make sure we both understand each other:
> > you take the sort template, stick in a delegate, mix for five minutes
> > let is simmer for one more and viola: you got the exact same thing if
> > you would have written sort as a regular function which receives a
> > delegate parameter.
> > the template takes the symbol it receives (the delegate name) and
> > "embeds" it in the sort instance.
> > both ways you get:
> > 1) a call to sort
> > 2) a call to the delegate
> > the only difference is the number of parameters the sort function
> > receives when called.
> > the regular sort function can run different delegates at runtime (this
> > is what you call "dynamic". the template has the call to the specific
> > delegate embedded so for different delegates you need different sort
> > instances.
> Good. I think I understand. I want to be very sure, so I have a question from this code.
> int array[] = [1, 2, 3];
> int x = 5;
> sort!((int a, int b) { return a + x < b + x; })(array);
> Two questions not one ^_^
> 1. Is the code inside sort!() as powerful as delegate?
It depends what you mean by "powerful." Passing a comparator as a template parameter,
as with sort!(), means that the choice of comparator must be made at compile-time rather
than run-time. This may be problematic in some instances.
> 2. Is the call inside sort direct or indirect? Thank you, Dee Girl
Do you mean inline or not inline? It's hard to say. DMD's inlining mechanism is a bit
quirky, so it could go either way. For example, I've had struct member functions which
would be inlined if static and not inlined if not static, and other member functions which
were the reverse. I haven't found any way to be sure other than trying it out and examining
the ASM from the object file.
Sean
More information about the Digitalmars-d
mailing list