How do you declare an extern “C” function pointer

感情迁移 提交于 2019-12-04 01:57:31

You can try including cmath instead, and using static_cast<double(*)(double)>(std::log) (cast necessary to resolve to the double overload).

Otherwise, you will limit your function to extern C functions. This would work like

extern "C" typedef double (*ExtCFuncPtr)(double);

double foo(double num, ExtCFuncPtr func) {
  return 65.4;
}

Another way is to make foo a functor

struct foo {
  typedef double result_type;
  template<typename FuncPtr>
  double operator()(double num, FuncPtr f) const {
    return 65.4;
  }
};

Then you can pass foo() to boost::bind, and because it's templated, it will accept any linkage. It will also work with function objects, not only with function pointers.

Try using a typedef:

extern "C" {
  typedef double (*CDoubleFunc)(double);
}

double foo(double num, CDoubleFunc func) {
  return 65.4;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!