将std :: bind函数指针转换为void *并返回

时间:2016-12-04 23:55:45

标签: c++ casting stdbind

让我们说我想绑定一个函数,然后将它作为void *传递给另一个函数,并将其转换回来。

我有一个全局函数,我尝试转换函数:

void function_wrapper(void * func){

std::function<void(const std::vector<double>&, const std::vector<double>&)> * function = reinterpret_cast<std::function<void(const std::vector<double>&, const std::vector<double>&)> *>(func);

std::function<void(const std::vector<double>&, const std::vector<double>&)> function_ = *function;
}
同时,

func创建为:

auto func = std::bind(&MyObj<dim>::myfunc, this, _1, _2);

此处dim是一个等于2的模板化整数,function_wrapper通过

调用
function_wrapper((void *)(&func));

与此同时,myfuncMyObj<dim>的方法,其类型为:

void myfunc(const std::vector<double>& x, std::vector<double>& F) const;

当我尝试按上述方法调用function_wrapper时,我在取消引用它时会出现以下错误:

Exception thrown: read access violation.

std::_Func_class<void,std::vector<double,std::allocator<double> > const & __ptr64,std::vector<double,std::allocator<double> > const & __ptr64>::_Getimpl(...) returned 0xFFFFFFFFFFFFFFFF.

If there is a handler for this exception, the program may be safely continued.

我认为我的演员输入错误,但我不知道声明类型的正确方法。这样做的正确方法是什么?

0 个答案:

没有答案
相关问题