関数ポインタをキャスト

Cで、

int djacobi(void*, int*, int*, double*, double*, double*);
void target_func(int*, int*, double*, double*);
int main () {
  int a, b;
  double c, d, e;
  djacobi(target_func, &a, &b, &c, &d, &e);
  return 0;
}

のよーなキャスト、つまり "void (*)(int*, int*, double*, double*)" から "void*" へのキャストは許される。
しかしC++では許されない。

extern "C" {
  int djacobi(void*, int*, int*, double*, double*, double*);
  void target_func(int*, int*, double*, double*);
}
int main () {
  int a, b;
  double c, d, e;
  djacobi(target_func, &a, &b, &c, &d, &e);
  return 0;
}
$ g++ -c hoge.cc
hoge.cc: In function ‘int main()’:
hoge.cc:8: error: invalid conversion from ‘void (*)(int*, int*, double*, double*)’ to ‘void*’
hoge.cc:8: error:   initializing argument 1 of ‘int djacobi(void*, int*, int*, double*, double*, double*)


これを避けるためには、reinterpret_castを使う。

extern "C" {
  int djacobi(void*, int*, int*, double*, double*, double*);
  void target_func(int*, int*, double*, double*);
}
int main () {
  int a, b;
  double c, d, e;
  djacobi(reinterpret_cast<void*>(target_func), &a, &b, &c, &d, &e);
  return 0;
}

これで通った。
コンパイル通ったけど動くのかな……w