Function pointers/delegates default args were stealth removed?

Walter Bright newshound2 at digitalmars.com
Mon Aug 27 14:03:46 PDT 2012


On 8/27/2012 3:32 AM, Manu wrote:
> Here's an advanced trick I use a lot since D doesn't extern to static C++
> methods (heavily simplified, this is way out of context):
>
> struct CPPClass
> {
>      this()
>      {
>          // not my actual code, but effectively, write 'this' and the C++ method
> pointer into a delegate on initialisation [I wrap this process up using magic]
>          void** pDelegate = cast(void**)&cppNonVirtualMethod;
>          pDelegate[0] = this;
>          pDelegate[1] = pCPPMethodPointer;
>      }
>
>      void delegate(int x = 0) cppNonVirtualMethod; // C++ methods often have
> default args

        void delegate(int x) cppNonVirtualMethod;
        void callCppNonVirtualMethod(int x) { (*cppNonVirtualMethod)(x); }
        void callCppNonVirtualMethod() { callCppNonVirtualMethod(0); }

With inlining on, the calls to the second overload should disappear, and you 
have the same code generated as you would for the default arg method. You could 
probably reduce the typing with some sort of mixin template, but this is the 
basic idea.

> private:
>      // C++ method pointer received from foreign code during initialisation
>      static void* pCPPMethodPointer;
> }
>




More information about the Digitalmars-d mailing list