函数指针void*加载dlsym(),我可以把它投射到std::function?
假设lib中的函数声明是
int func(int);
using FuncType = std::function<int(int)>;
FuncType f = dlsym(libHandle, "func"); // can this work?
FuncType f = reinterpret_cast<FuncType>(dlsym(libHandle, "func")); // how about this?
Run Code Online (Sandbox Code Playgroud)
asc*_*ler 14
不,函数类型int(int)和类类型std::function<int(int)>是两种不同的类型.无论何时使用dlsym,都必须将结果指针仅转换为指向符号实际类型的指针.但在那之后,你可以用它做你想要的.
特别是,您可以std::function从指向函数的指针构造或赋值:
using RawFuncType = int(int);
std::function<int(int)> f{
reinterpret_cast<RawFuncType*>(dlsym(libHandle, "func")) };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
956 次 |
| 最近记录: |