www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Help on passing a function to a function pointer.

reply Kirill <kirill.saidov mail.com> writes:
I am exploring GLFW with D (bindbc-glfw). I faced the following 
problem:

I need to set the frame buffer size callback:
`glfwSetFramebufferSizeCallback(GLFWwindow*, extern (C) void 
function(GLFWwindow*, int, int) nothrow)`

I have created a function:
`extern (C) void setFrameBufferSizeCallback(GLFWwindow* window, 
int w, int h) nothrow {
         glfwViewport(0, 0, w, h);
}`

And then:
`glfwSetFramebufferSizeCallback(window, 
&setFrameBufferSizeCallback)`

DMD produces the following error:
`source/display.d(59,32): Error: function pointer 
glfwSetFramebufferSizeCallback(GLFWwindow*, extern (C) void 
function(GLFWwindow*, int, int) nothrow)
is not callable using argument types
(GLFWwindow*, extern (C) void delegate(GLFWwindow* window, int w, 
int h) nothrow)

source/display.d(59,32): cannot pass argument
&this.frameBufferSizeCallback of type extern (C) void 
delegate(GLFWwindow* window, int w, int h) nothrow to parameter 
extern (C) void function(GLFWwindow*, int, int) nothrow
dmd failed with exit code 1.
`

What should I do? I have read the available information on D 
function pointers, but I still can't get it working. What am I 
missing?
Mar 23
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 24 March 2021 at 02:41:39 UTC, Kirill wrote:
 DMD produces the following error:
 `source/display.d(59,32): Error: function pointer 
 glfwSetFramebufferSizeCallback(GLFWwindow*, extern (C) void 
 function(GLFWwindow*, int, int) nothrow)
 is not callable using argument types
 (GLFWwindow*, extern (C) void delegate(GLFWwindow* window, int 
 w, int h) nothrow)
You're trying to pass a pointer to a member function (a "delegate"), but it expects a pointer to a non-member function. Either make the function `static` or move it outside the class/struct it's currently in.
Mar 23
parent Kirill <kirill.saidov mail.com> writes:
On Wednesday, 24 March 2021 at 03:06:15 UTC, Paul Backus wrote:
 On Wednesday, 24 March 2021 at 02:41:39 UTC, Kirill wrote:
 DMD produces the following error:
 `source/display.d(59,32): Error: function pointer 
 glfwSetFramebufferSizeCallback(GLFWwindow*, extern (C) void 
 function(GLFWwindow*, int, int) nothrow)
 is not callable using argument types
 (GLFWwindow*, extern (C) void delegate(GLFWwindow* window, int 
 w, int h) nothrow)
You're trying to pass a pointer to a member function (a "delegate"), but it expects a pointer to a non-member function. Either make the function `static` or move it outside the class/struct it's currently in.
Thank you! That worked!
Mar 23