【发布时间】:2017-07-08 19:16:09
【问题描述】:
我有一个问题。我正在尝试将 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;
}
我尝试的每一个演员都没有成功。
我知道我可以在这个例子中发送std::function,但它是为了更大的东西,我正在研究一个例子来让它在这里工作。
void* 的全部含义,是为了有时使用它,在这些情况下,当你不知道你会收到什么时,然后将它转换为你需要的特定用途。
谢谢!
【问题讨论】:
-
你为什么这样做?
void*来自什么(一些dlsym?)。请注意,闭包比一个指针大。 -
一些 dsystem 库。如果我不必。我不会这样做
-
不可能将
void*存储在std::function中,因为它不可调用。您对target的调用将始终返回一个空指针。 -
我明白了..所以没有选择双向投射它?
-
我必须使用指向 std 函数的指针?
标签: c++11 casting void-pointers std-function