首先我不得不说我必须知道 std::bind 返回的数据类型。
我有一个结构体定义为
typedef struct
{
UINT ID;
CString NAME;
boost::any Func;// 'auto' doesn't work here
} CALLBACK;
CALLBACK CallBackItems[];
Func是一个函数持有者,我希望它持有不同类型的回调函数。
我在某个地方初始化 CallBackItems,如下所示:
CallBackItems[] =
{
{ 1, L"OnReady", std::bind(&CPopunderDlg::OnReady, pDlg) },
{ 2, L"CustomFunction",std::bind(&CPopunderDlg::OnFSCommond, pDlg,_1,_2) }
//................... more items here
};
当我尝试在每个回调中使用“Func”时,我必须先对其进行强制转换,然后像函数一样使用它。到目前为止我尝试过:
//CallBackItems[0].Func is binded from pDlg->OnReady(), pDlg->OnReady() works here,
boost::any_cast<function<void()>>(CallBackItems[0].Func)();
((std::function<void()>)(CallBackItems[0].Func))();
它们都不起作用,有人知道如何从 std::bind 转换返回的变量吗?