仿函数传递作为函数指针 [英] passing functor as function pointer

查看:308
本文介绍了仿函数传递作为函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个C ++应用程序使用一个C库,发现我在以下情况下(我知道我的C,但我是相当新的C ++)的自我。在C面我有功能的集合,它接受一个函数指针作为他们的论点。在C ++的一边,我与它有相同签名的C函数所需的函数指针仿函数对象。有没有办法使用C ++函数对象作为函数指针传递给C函数?

I'm trying to use a C library in a C++ app and have found my self in the following situation (I know my C, but I'm fairly new to C++). On the C side I have a collection of functions that takes a function pointer as their argument. On the C++ side I have objects with a functor which has the same signature as the function pointer needed by the C function. Is there any way to use the C++ functor as a function pointer to pass to the C function?

推荐答案

您不能直接指针传递给一个C ++仿函数对象作为一个函数指针到C code
(甚至C ++ code)。

You cannot directly pass a pointer to a C++ functor object as a function pointer to C code (or even to C++ code).

此外,可移植传递回调至C code它需要至少声明
作为的externC非成员函数。
至少,因为一些API需要特定的函数调用约定,因而
附加声明的改性剂。

Additionally, to portably pass a callback to C code it needs to be at least declared as an extern "C" non-member function. At least, because some APIs require specific function call conventions and thus additional declaration modifiers.

在许多环境中C和C ++具有相同的调用约定,只有不同
在名称重整,所以任何全局函数或静态成员将工作。
但你仍然需要包装调用操作符()的正常功能。

In many environments C and C++ have the same calling conventions and differ only in name mangling, so any global function or static member will work. But you still need to wrap the call to operator() in a normal function.


  • 如果您的函子无状态(这是一个对象只是为了满足一些正规
    要求等):

  • If your functor has no state (it is an object just to satisfy some formal requirements etc):

class MyFunctor {
  // no state
 public:
  MyFunctor();
  int operator()(SomeType &param) const;
}

您可以编写创建函子与执行其正常的externC功能
运算符()。

you can write a normal extern "C" function which creates the functor and executes its operator().

extern "C" int MyFunctorInC(SomeType *param)
{
  static MyFunctor my_functor;
  return my_functor(*param);
}


  • 如果您的函子有状态,例如:

  • If your functor has state, eg:

    class MyFunctor {
      // Some fields here;
     public:
      MyFunctor(/* some parameters to set state */);
      int operator()(SomeType &param) const;
      // + some methods to retrieve result.
    }
    

    的C回调函数某种类型的用户状态参数(通常是无效*):

    and the C callback function takes some kind of user state parameter (usually void *):

    void MyAlgorithmInC(SomeType *arr,
                        int (*fun)(SomeType *, void *),
                        void *user_state);
    

    你可以写一个普通的externC功能,蒙上它的状态参数
    你的仿函数对象:

    you can write a normal extern "C" function which casts its state parameter to your functor object:

    extern "C" int MyFunctorInC(SomeType *param, void *user_state)
    {
      MyFunctor *my_functor = (MyFunctor *)user_state;
      return (*my_functor)(*param);
    }
    

    和使用这样的:

    MyFunctor my_functor(/* setup parameters */);
    MyAlgorithmInC(input_data, MyFunctorInC, &my_functor);
    


  • 否则只有正常的方式做到这一点
    (正常为没有在运行时产生机code等)
    是使用一些静态的(全局)或线程本地存储传递函子
    一个外部的C功​​能。
    这限制了你可以用code做什么,是丑陋的,但会工作。

  • Otherwise the only normal way to do it (normal as in "without generating machine code at runtime" etc.) is to use some static (global) or thread local storage to pass the functor to an extern "C" function. This limits what you can do with your code and is ugly but will work.

    这篇关于仿函数传递作为函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆