C++ 如何从boost::function对象获取函数指针
StackOverflow上还有其他解决方案,它们建议使用相同的方法将boost::function转换为函数指针。但同样的代码会给我带来错误。我是否需要介绍reinterpret_cast,或者有更清晰的解释为什么要这样做 下面是生成错误的示例代码:C++ 如何从boost::function对象获取函数指针,c++,boost,boost-bind,boost-function,C++,Boost,Boost Bind,Boost Function,StackOverflow上还有其他解决方案,它们建议使用相同的方法将boost::function转换为函数指针。但同样的代码会给我带来错误。我是否需要介绍reinterpret_cast,或者有更清晰的解释为什么要这样做 下面是生成错误的示例代码: void Do ( int (*callback) (MYType1 const*, MYType2* ) ) { MYType2 tmp; boost:: function <int(MYType1 const*)>
void
Do ( int (*callback) (MYType1 const*, MYType2* ) )
{
MYType2 tmp;
boost:: function <int(MYType1 const*)> fun = boost::bind( callback, _1, &tmp );
// My intention is to create c style function pointer out of `fun`
// but below line is generating error:
// error: cannot convert 'boost::function < int () (const MYType1*), std::allocator<void> >' to 'int (*)(const MYType1*)' in initialization
int (*c)(MYType1 const*) = &fun;
}
@Jonathon Reinhart的可能重复:为什么“c”的赋值初始化会产生错误,而它与您指出的解决方案相同?看起来您还没有理解链接到的帖子。正如那篇文章所说,在所有情况下都没有从boost::函数获取函数指针的方法。在您的代码中,C风格的函数指针应该如何知道tmp?