c/c++语言开发共享将代理传递给D中的外部C函数

如何在D中将代理传递给带有函数指针的外部C函数?

    让我交叉发布我在新闻组中所说的内容:

    如何使用函数指针将委托传递给外部C函数?

    你不能直接做到这一点,除非你可以修改C函数,然后你可以破解它,但委托和常规函数指针是完全不同的动物。

    但也许你可以魔术破解它。 注意:

    // a C function that needs a plain function extern(C) void test(void function() f) { // pretend this is implemented in C f(); } // just create a random delegate void delegate() foo(int a) { return { import std.stdio; writeln(a); }; } // what we want to work void main() { auto dg = foo(10); dg(); // works //test(dg); // won't work test(bindDelegate(dg)); // we want this } // transform delegate into pointer.. import std.traits; auto bindDelegate(T, string file = __FILE__, size_t line = __LINE__)(T t) if(isDelegate!T) { static T dg; dg = t; extern(C) static ReturnType!T func(ParameterTypeTuple!T args) { return dg(args); } return &func; } 

    bindDelegate所做的是为该特定调用创建一个特殊的静态变量和函数。 就好像我们写了一个单独的函数和全局来保存它。

    __FILE__ ,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ @

      以上就是c/c++开发分享将代理传递给D中的外部C函数相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/545554.html

      (0)
      上一篇 2021年1月10日
      下一篇 2021年1月10日

      精彩推荐