Search code examples
c++c++11initializer-list

Combining prefix &* operations in C++


In the answer to How to pass an initializer list as a function argument?, I see the expression:

return WaitForMultipleObjects(
    handles.size(), &*handles.begin(), wait_all, time
);

What's the point of the &*? Would the call have the same meaning without it?


Solution

    • In general, container.begin() returns an iterator
    • In general, &*container.begin() returns a pointer
    • WaitForMultipleObjects expects a HANDLE const*

    For std::initializer_list<> in particular, the iterator is defined to be a pointer; but A) this is not generalizable to other container types, and B) standard library implementations often use checked iterators in debug builds, which are necessarily UDTs.

    For the particular answer you linked to, the &* is not necessary because it is specifically using std::initializer_list<>; however, if wait_for_multiple_objects instead took a std::array<> or std::vector<>, or if it were made into a function template that could take any random-access container, it would indeed be necessary.