Function inlining?

RedZone garfieldrules at sbcglobal.net
Sun May 23 14:14:13 PDT 2010


Hello,

I've dabbled in D here and there, and love it.  From what I can see, though,
there's not exactly an "inline" keyword for function inlining.  I know that
the D compiler can do inlining by adding "-inline", but that doesn't really
allow one to mark specific functions for inlining.  Is there any way to
achieve manual inlining of a function?  I'm starting to think that possibly
mixins can do this, but I'm not sure.  If I understand the documentation, I
think it would work like this:

template mixin Foo(alias x, alias y)
{
   int add() {return x + y}
   int sub() {return x - y}
}

void main()
{
   int sum;
   int a = 5;
   int b = 2;

   mixin Foo!(a, b);
   sum = add();
}


If I understand the documentation, the compiler will inline whichever function
I call from the template mixin Foo, and leave out whichever functions I don't
call--correct?  Do I need to add on -inline for it to work?


More information about the Digitalmars-d-learn mailing list