Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you declare an extern "C" function pointer

So I have this code:

#include "boost_bind.h"
#include <math.h>
#include <vector>
#include <algorithm>

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

int main(int argc, char** argv) {
  std::vector<double> vec;
  vec.push_back(5.0);
  vec.push_back(6.0);
  std::transform(vec.begin(), vec.end(), vec.begin(), boost::bind(foo, _1, log));
}

And receive this error:

        return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
.............................................................^
%CXX-E-INCOMPATIBLEPRM, argument of type "double (* __ptr64 )(double) C" is
          incompatible with parameter of type "double (* __ptr64 )(double)"
          detected during:
            instantiation of ...5 pages of boost

So this error is because 'log' is extern "C"'d in math.h

I was wondering how to declare my function pointer argument in foo() so it handles extern "C"'d functions.

like image 763
Salgar Avatar asked Aug 17 '09 17:08

Salgar


People also ask

How do you declare a pointer to a function?

int foo(int); Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, by putting a * operator between int and foo(int) should create a pointer to a function i.e. int * foo(int);

Is it possible to declare function pointers?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

Can we extern function in C?

The extern keyword in C and C++ extends the visibility of variables and functions across multiple source files. In the case of functions, the extern keyword is used implicitly. But with variables, you have to use the keyword explicitly.


1 Answers

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.

like image 142
Johannes Schaub - litb Avatar answered Sep 28 '22 09:09

Johannes Schaub - litb