How to pass a list of initializers as an argument to a function?

Basically, I want to do something like this:

HANDLE hThread1 = CreateThread(...); HANDLE hThread2 = CreateThread(...); HANDLE hThread3 = CreateThread(...); ... WaitForMultipleObjects( 3, {hThread1,hThread2,hThread3}, FALSE, INFINITE ); 

instead of this:

 HANDLE hThread[3]; hThread[0] = CreateThread(...); hThread[1] = CreateThread(...); hThread[2] = CreateThread(...); ... WaitForMultipleObjects( 3, hThread, FALSE, INFINITE ); 

The only solution I found is to use std::initializer_list , but obviously WaitForMultipleObjects() does not accept std::initializer_list

+4
source share
1 answer

Then write a wrapper.

 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 ); } 

Now you can do:

 wait_for_multiple_objects({ handle1, handle2, handle3 }); 

This obviously requires a C ++ 11 compiler that supports initializer_list . std::vector<HANDLE> may be the best type of argument if you expect to pass an existing one. Or a more general iterator / range interface, but this is left as an exercise for the reader.

+5
source

Source: https://habr.com/ru/post/1379577/


All Articles