Mum*_*wiz 4 casting void-pointers c++11 std-function
我有一个问题。我正在尝试将 void* 转换为 std::function 。这只是一个简单的例子,任何建议将不胜感激
#.h file
class Example {
public:
Example();
int foo(void* hi);
int fooFunc(std::function<int(int, int)> const& arg, int x, int y) {
foo(arg.target<void*>(), x, y);
return 2;
}
};
#.cpp file
Example::Example() {
}
int Example::foo(void * func, int x, int y)
{
//cast back to std::function
func(x, y);
std::cout << "running in foo: " << a << "\n";
return a;
}
Run Code Online (Sandbox Code Playgroud)
我尝试的每一次选角都不起作用。
我知道我可以在这个例子中发送一个std::function,但这是为了更大的事情,我正在研究一个例子以使其在这里工作。
,的全部含义void*是有时在这些情况下使用它,当您不知道您将收到什么时,然后将其转换为您需要的特定用法。
谢谢!
你不能。
您可以将数据指针转换为void*您开始时使用的相同指针类型,然后再转换回该类型。std::function不是指针类型,因此强制转换在静态上是无效的,并且它与您开始时的情况不同。.target您已经从of 类型开始void(*)(),但它不是数据指针,而是函数指针,因此将其转换为void*实现定义的。
你可以:
void(*)()无论如何。适用于大多数(但不是全部)平台。void(*)()而不是void*作为通用函数指针(您可以将其转换为其他函数类型)。| 归档时间: |
|
| 查看次数: |
5634 次 |
| 最近记录: |