如何将初始化列表作为函数参数传递? [英] How to pass an initializer list as a function argument?
问题描述
基本上,我想做这样的事情:
Basically, I'm looking to do something like this:
HANDLE hThread1 = CreateThread(...);
HANDLE hThread2 = CreateThread(...);
HANDLE hThread3 = CreateThread(...);
...
WaitForMultipleObjects( 3, {hThread1,hThread2,hThread3}, FALSE, INFINITE );
代替此:
HANDLE hThread[3];
hThread[0] = CreateThread(...);
hThread[1] = CreateThread(...);
hThread[2] = CreateThread(...);
...
WaitForMultipleObjects( 3, hThread, FALSE, INFINITE );
我发现的唯一解决方案是使用 std :: initializer_list
,但是显然 WaitForMultipleObjects()
不接受 std ::initializer_list
The only solution I've found is using std::initializer_list
, but obviously WaitForMultipleObjects()
doesn't doesn't accept an std::initializer_list
推荐答案
然后编写一个包装器.
DWORD wait_for_multiple_objects(
std::initializer_list<HANDLE> handles,
bool wait_all = false, DWORD time = INFINITE
) {
return WaitForMultipleObjects(
handles.size(), &*handles.begin(), wait_all, time
);
}
现在您可以这样做:
wait_for_multiple_objects({ handle1, handle2, handle3 });
这显然需要支持 initializer_list
的C ++ 11编译器.如果您希望传递已经存在的参数,则 std :: vector< HANDLE>
可能是该参数的更好类型.或更通用的迭代器/范围接口,但这留给读者练习.
This obviously requires C++11 compiler that supports initializer_list
. std::vector<HANDLE>
might be a better type for the argument if you expect to pass an already-existing one. Or a more generic iterator/range interface, but that's left as an exercise for the reader.
这篇关于如何将初始化列表作为函数参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!