为了调试目的,我想打印存储在我的std :: function中的函数指针的地址.我可以保证std :: function将指向c风格的函数或lambda.有没有办法做到这一点?
否则,我将不得不在添加函数指针时将其存储在内存中并修改所有lambdas.
一些示例代码:
std::function func = commands.front();
void * fp = get_fn_ptr<0>(func);
void * bb = &glBindBuffer;
printf("bb is %x\n", bb); // Outputs 5503dfe0
printf("fp is %x\n", fp); // Should be the same as above, but outputs 4f9680
解决方法:
您的版本不起作用,因为您链接的答案是提供函数的包装,以提供您可以使用的独立版本,无论源是functor,函数指针还是std :: function.
在您的情况下,您可以使用std :: function的目标函数:
void foo(){}
std::function f = foo;
auto fp = *f.target();
auto bb = &foo;
printf("bb is %x\n", bb);
printf("fp is %x\n", fp);
输出:
bb is 80487e0
fp is 80487e0
标签:c,c11,lambda,stl
来源: https://codeday.me/bug/20190830/1765377.html