How to pass an initializer list as a function argument?

后端 未结 1 665
南笙
南笙 2021-01-06 10:28

Basically, I\'m looking to do something like this:

HANDLE hThread1 = CreateThread(...);
HANDLE hThread2 = CreateThread(...);
HANDLE hThread3 = CreateThread(.         


        
相关标签:
1条回答
  • 2021-01-06 10:50

    Write a wrapper, then.

    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 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.

    0 讨论(0)
提交回复
热议问题