How do you declare an extern “C” function pointer

后端 未结 2 1290
青春惊慌失措
青春惊慌失措 2021-02-20 03:51

So I have this code:

#include \"boost_bind.h\"
#include 
#include 
#include 

double foo(double num, double (*func)(         


        
相关标签:
2条回答
  • 2021-02-20 04:21

    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.

    0 讨论(0)
  • 2021-02-20 04:21

    Try using a typedef:

    extern "C" {
      typedef double (*CDoubleFunc)(double);
    }
    
    double foo(double num, CDoubleFunc func) {
      return 65.4;
    }
    
    0 讨论(0)
提交回复
热议问题