gpt4 book ai didi

function - 在 Dlang 中传递函数指针

转载 作者:行者123 更新时间:2023-12-04 14:27:40 28 4
gpt4 key购买 nike

我正在尝试使用 Dlang 运行 OpenGL 示例。

void onError(int code, const(char)* text) nothrow
{
}
用法:
glfwSetErrorCallback(&onError);
绑定(bind)代码:
__gshared {

da_glfwSetErrorCallback glfwSetErrorCallback;

...

extern( C ) @ nogc nothrow {

alias da_glfwSetErrorCallback = GLFWerrorfun function( GLFWerrorfun );

...

alias GLFWerrorfun = void function( int, const( char )* );
我得到以下编译器错误:
Error: function pointer glfwSetErrorCallback (extern (C) void function(int, const(char)*) nothrow) is not callable using argument types (void function(int code, const(char)* text) nothrow)
编译器:2.065.0

最佳答案

来自 interfacing to C guidelines关于回调:

D can easily call C callbacks (function pointers), and C can call callbacks provided by D code if the callback is an extern(C) function, or some other linkage that both sides have agreed to (e.g. extern(Windows)).



所以我认为你需要你的 onError要声明为 extern(C) 的函数为了让它匹配类型签名。

关于function - 在 Dlang 中传递函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971525/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com