function.prototype.bind()在C中的替代方案(不是C++)
在我的C程序中,我需要将回调功能传递给第三方库。图书馆用一些参数调用此回调。但是,我需要在此回调中曝光一个变量。该变量可以从内部的函数范围访问,我将设置回调。
在JS中,我可以轻松地使用.bind()
,或多或少地这样解决此问题(pseudocode):
func my_callback(int a, int b) {
printf("A: %d\n", a);
printf("B: %d\n", b);
}
func new_instance() {
int a = 1;
// setup_callback expects (void(*)(int b)) as an argument
setup_callback(my_callback.bind(null, a));
}
有两个主要限制:
- 变量
a
不能全局 -它在new_instance
过程中声明并初始化。 - 它必须是C解决方案(理想情况下是ANSI),而不是C ++。
玩了一段时间(功能指针)后,我似乎没有更接近可行的解决方案...
In my C program, I need to pass a callback function to a 3rd party library. The library calls this callback with a few arguments. However, I need to expose one more variable to this callback. This variable is accessible in the scope of the function from within I'm setting the callback.
In JS, I'd easily solve this with .bind()
, more or less like this (pseudocode):
func my_callback(int a, int b) {
printf("A: %d\n", a);
printf("B: %d\n", b);
}
func new_instance() {
int a = 1;
// setup_callback expects (void(*)(int b)) as an argument
setup_callback(my_callback.bind(null, a));
}
There are two main restrictions:
- Variable
a
cannot be global - it is declared and initialized inside thenew_instance
procedure. - It has to be a C solution (ideally ANSI), not C++.
After playing with it (function pointers) for a while, I don't seem to be any closer to a workable solution...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要求关闭,关闭不是C语言的功能。由于您无法控制使用回调的API,因此没有解决方法可以使您可以通过回调获得附加的本地变量。
如果您是控制API的,那么可以使回调接口与该函数一起接受附加参数,并将其与其一起存储,并在其旁边作为参数传递给它函数被调用。通常,类型
void *
的参数用于此目的,因为您可以传达任何类型的数据。但是,即使那样,您仍需要小心。您可以以这种方式提供本地变量的 value ,但是如果您提供了指针 a local变量,则只能在该变量的使用寿命中使用,不超过包含它的函数的执行时间(在某些情况下会较短)。
You are asking for a closure, and closures are not a feature of the C language. Since you're not in control of the callback-using API, there is no workaround to allow you to get an additional local variable through to the callback.
If you were in control of the API, then it would be possible to make the callback interface accept an additional parameter along with the function, to be stored alongside it and be passed to it as an argument when the function is called. Typically a parameter of type
void *
is used for such a purpose, as you can convey data of any type that way.Even then, however, you need to be careful. You could provide the value of a local variable that way, but if you provided a pointer to a local variable then that would only be usable during the lifetime of that variable, which is not longer than the execution of the function containing it (and would be shorter under some circumstances).
C不支持这一时期。可以做的是将“绑定”变量存储在可访问的范围中,并将其暴露于可用的代码中,该代码将创建闭合。
它具有存储状态的缺点,因此在多线程上下文中共享数据时可能会变得凌乱。
应该足够吗?取决于上下文,但请记住,这绝不是函数对象,它仅仅是存储的状态。
可以改进吗?取决于,但是如果可以使用回调传递的
b
参数可以以某种方式识别其呼叫者,则可能可以创建某个此类回调状态的容器来彼此分开。死去的简单解决方案将是这样的。再说一次,我不能说就足够了。
C does not support that, period. What can be done instead is to store the "bound" variable in a scope accessible to the callback and expose it to the code that would create the closure were it's available.
It has the drawbacks of storing state, thus it might get messy when sharing data e.g. in multithreaded contexts.
Should it suffice? Depends on context, but bear in mind this is no function object by any means, it is merely a stored state.
Can it be improved? Depends, but if the
b
parameter passed by the API using the callback can utilized to somehow identify its caller, then maybe some container of such callback states can be created to separate them from each other.The dead-simple solution would be sth like this. Again, I cannot tell it this suffices or not.