什么是特定的std :: bind的返回数据类型? [英] What exactly is the returned data type for a particular std::bind?
问题描述
首先我必须说我必须知道从std :: bind返回的数据类型。
First I have to say I have to know the returned data type from std::bind.
我有一个结构体,定义为
I have a struct which is defined as
typedef struct
{
UINT ID;
CString NAME;
boost::any Func;// 'auto' doesn't work here
} CALLBACK;
CALLBACK CallBackItems[];
Func是一个函数持有者,我想让它持有不同类型的回调函数。
Func is a function holder, I want it to hold different kinds of callback function.
我在这里初始化CallBackItems:
Somewhere I initialize CallBackItems like this:
CallBackItems[] =
{
{ 1, L"OnReady", std::bind(&CPopunderDlg::OnReady, pDlg) },
{ 2, L"CustomFunction",std::bind(&CPopunderDlg::OnFSCommond, pDlg,_1,_2) }
//................... more items here
};
当我尝试在每个CALLBACK中使用'Func'时,它像一个函数。到目前为止我试过:
When I try to use the 'Func' in each CALLBACK I have to cast it first and then use it like a function. So far I tried:
//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转换返回的变量? p>
none of them work, anybody knows how to cast the returned variables from std::bind?
推荐答案
std :: bind
返回的类型未指定:
20.8.9.1.3函数模板bind [func.bind.bind]
sup> 1 ...
template< class F,class ... BoundArgs>
br>
未指定 bind(F& f,BoundArgs& ... bound_args) ;
您可以使用 std :: function
以存储它们,例如
You can use std::function
to store them, e.g.
void f( int ) {}
std::function< void(int) > f2 = std::bind(&f, _1);
在你的情况下,这意味着你可能需要转换类型当你存储结果从 std :: bind
:
In your case, this mean you could need to cast the type when you store the result from std::bind
:
CallBackItems[] =
{
{ 1, L"OnReady", std::function< void() >( std::bind(&CPopunderDlg::OnReady, pDlg) ) },
{ 2, L"CustomFunction", std::function< void(int,int) >( std::bind(&CPopunderDlg::OnFSCommond, pDlg,_1,_2) ) },
};
,然后取回:
boost::any_cast<std::function<void()>>(CallBackItems[0].Func)();
这篇关于什么是特定的std :: bind的返回数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!