Inlining of function(){...}()

Clemens eriatarka84 at gmail.com
Mon Apr 26 09:48:27 PDT 2010


BCS Wrote:

> Hello Clemens,
> 
> > Mathias Laurenz Baumann Wrote:
> > 
> >> Greetings,
> >> 
> >> The following code seems to create a new function call:
> >> 
> >> module test;
> >> void main(char[][] args)
> >> {
> >> return function(int i) { return i+2; }(1);
> >> }
> > I guess the compiler could peephole-optimize that. Though: how common
> > would that idiom be? Why create a function literal just to call it on
> > the spot? Why not use a nested function in the first place?
> > 
> 
> 
> int Wrap(int function() fn, int i) {
> pre();
> auto r = fn(i);
> post();
> return r;
> }
> 
> 
> void main()
> {
>    Wrap(function(int i) { return i+2; },0);
> }
> 
> after Wrap is inlined, you get the same case as above

Ah yes, good point. In that light it seems like a very useful optimization.



More information about the Digitalmars-d mailing list