[英]How can I pass an extern(C) function literal?
說我正在接口C.
這是接口的包裝功能。
@property extern(C) void onEvent(void function(InterfaceStruct*, int, int, int) nothrow callback)
{
interfaceSetCallback(handle, callback);
}
都好。
wrapper.onEvent = function void (InterfaceStruct*, int x, int y, int z) nothrow
{
if (x == 11) doSomething();
};
哦哦:
Error: function foo.bar.onEvent (void function(InterfaceStruct*, int, int, int) nothrow callback) is not callable using argument types (void function(InterfaceStruct* _param_0, int x, int y, int z) nothrow @nogc @safe)
所以,它希望我的函數文字是extern(C)。 那我該怎么辦呢? 我找不到任何辦法這樣做。
您可以簡單地使用onEvent分配,而不是提供整個函數定義
wrapper.onEvent = (a, x, y, z)
{
if (x == 11) doSomething();
};
D將自動為其指定正確的類型。
此外,您的代碼實際上應該給您一個語法錯誤,因為當將它用於函數指針定義時實際上不允許使用extern(C)。
您也可以為函數指針類型定義別名,並將賦值轉換為它,如下所示:
alias EventCallback = extern(C) void function(InterfaceStruct*, int, int, int) nothrow;
@property extern(C) void onEvent(EventCallback callback)
{
interfaceSetCallback(handle, callback);
}
// ...
wrapper.onEvent = cast(EventCallback) function void(InterfaceStruct*, int x, int y, int z) nothrow
{
if (x == 11) doSomething();
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.