[英]Why is this function seen as a delegate?
In an attempt to wrap a C function taking callbacks, I encountered the problem of member functions being seen as delegates. 在尝试包装带有回调的C函数时,我遇到了将成员函数视为委托的问题。 The C function wouldn't take delegates, so I settled on something else instead:
C函数不会接受委托,因此我选择了其他方法:
extern(C) void onMouse(void delegate(int, int, int) nothrow callback)
{
glfwSetMouseButtonCallback(handle,
function void (GLFWwindow* h, int button, int action, int mods) nothrow
{
callback(button, action, mods);
}
);
}
As you can see, I'm passing to the callback setting function a function literal calling a delegate (which would be the member function I pass here). 如您所见,我正在向回调设置函数传递一个调用委托的函数文字(这将是我在此处传递的成员函数)。
However, it doesn't end up as I expect: 但是,它并没有像我期望的那样结束:
Error: function pointer glfwSetMouseButtonCallback (GLFWwindow*, extern (C) void function(GLFWwindow*, int, int, int) nothrow) is not callable using argument types (GLFWwindow*, void delegate(GLFWwindow* h, int button, int action, int mods) nothrow @system)
In the error, it shows the second parameter as being as the type void delegate
. 在错误中,它显示第二个参数为类型
void delegate
。
So. 所以。 My question is: why does this happen?
我的问题是:为什么会这样? As you can clearly see, it says
function void
in the code. 如您所见,代码中的
function void
。
NOTE: I've seen this: Pass delegates to external C functions in D . 注意:我已经看到了: 将委托传递给D中的外部C函数 。 The solution is apparently a hack.
解决方案显然是黑客。 But I will try it if I cannot find a workaround on the internet.
但是,如果无法在Internet上找到解决方法,我会尝试一下。
Even though you declare it as function, it cannot be. 即使您将其声明为函数,也不能。 It relies on the parameter you pass to
onMouse
. 它依赖于您传递给
onMouse
的参数。 That parameter is a local variable, and accessing them in a function body makes them a delegate. 该参数是一个局部变量,在函数体中对其进行访问将使它们成为委托。 All you can do is change the parameter to
function
and pass it with &callback
(you'd need to add GLFWwindow as parameter then). 您所要做的就是将参数更改为
function
并通过&callback
传递它(然后您需要添加GLFWwindow作为参数)。
Alternatively, you can create a global list in which this callback puts events which you can then process in your main loop. 另外,您可以创建一个全局列表,此回调将在其中放入事件,然后可以在主循环中处理这些事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.