简体   繁体   English

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

[英]Cast std::bind function pointer to void* and back

Let's say I want to bind a function and then pass it to another function as a void *, and cast it back. 假设我想绑定一个函数,然后将它作为void *传递给另一个函数,并将其转换回来。

I have a global function in which I try to cast the function: 我有一个全局函数,我尝试转换函数:

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, meanwhile, is created as: 与此同时,func被创建为:

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

Here dim is a templated integer equal to 2 And function_wrapper is called via 这里dim是一个等于2的模板化整数,而function_wrapper则被称为via

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

Meanwhile, myfunc is a method of MyObj<dim> with type: 同时, myfuncMyObj<dim>一种方法,类型为:

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

When I try to call function_wrapper as described above, I get the following error when I dereference it: 当我尝试按上述方法调用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.

I presume I have the typing wrong of my cast, but I don't know the correct way to declare the type. 我认为我的演员输入错误,但我不知道声明类型的正确方法。 What is the proper way to do this? 这样做的正确方法是什么?

The result of a std::bind is not a std::function , as the shown code assumes. std::bind的结果不是std::function ,如所示代码所假设的那样。

As such, when the shown code casts the pointer to the return value from std::bind to a void * , then casts it back to a pointer to a std::function , then invokes or accesses the function object, this results in undefined behavior. 因此,当显示的代码将指针转换为从std::bindvoid *的返回值时,然后将其强制转换为指向std::function的指针,然后调用或访问函数对象,这会导致未定义行为。

[func.bind.isbind] specifies std::bind as follows: [func.bind.isbind]指定std::bind ,如下所示:

template<class F, class... BoundArgs> unspecified bind(F&& f, BoundArgs&&... bound_args); template <class F,class ... BoundArgs> unspecified bind(F && f,BoundArgs && ... bound_args);

With the return value of: "A forwarding call wrapper g with a weak result type (20.9.2). The effect of g(u1, u2, ..., uM) shall be INVOKE (fd, std::forward(v1), std::forward(v2), ..., std::forward(vN), result_of_t)". 返回值为:“具有弱结果类型的转发调用包装器g (20.9.2).g(u1,u2,...,uM)的效果应为INVOKE(fd,std :: forward(v1) ),std :: forward(v2),...,std :: forward(vN),result_of_t)“。

That's it. 而已。 The class returned by std::bind is unspecified, and although it is certainly permitted for a given C++ implementation to, somehow, produce a std::function , it is not required to do so. std::bind返回的类是未指定的,尽管某个C ++实现肯定允许以某种方式生成std::function ,但不需要这样做。

But, the solution is simple: explicitly assign the return value from std::bind to an appropriate std::function . 但是,解决方案很简单:显式地将std::bind的返回值赋给适当的std::function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM