如何声明外部“C” 函数指针
所以我有这个代码:
#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));
}
并收到这个错误:
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
所以这个错误是因为'log'在 math.h 中是 extern "C"'d
我想知道如何在 foo() 中声明我的函数指针参数,以便它处理 extern "C" ”的功能。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试包含
cmath
,并使用static_cast(std::log)
(需要解析为双重载)。
否则,您将把您的函数限制为
extern C
函数。 这将像另一种方法是使 foo 成为函子
然后您可以将 foo() 传递给 boost::bind ,因为它是模板化的,它将接受任何链接。 它也适用于函数对象,而不仅仅是函数指针。
You can try including
cmath
instead, and usingstatic_cast<double(*)(double)>(std::log)
(cast necessary to resolve to thedouble
overload).Otherwise, you will limit your function to
extern C
functions. This would work likeAnother way is to make
foo
a functorThen you can pass
foo()
toboost::bind
, and because it's templated, it will accept any linkage. It will also work with function objects, not only with function pointers.尝试使用 typedef:
Try using a typedef: