Search code examples
c++for-loopfor-range

could someone explain that C++ code for me?


In the following code what is the meaning of colon? and where callback function came from?

using void_callback_f = void (*)();
std::vector<void_callback_f> _reload_callbacks;

void Reload() {
    for (const auto& callback : _reload_callbacks) {
        callback();
    }
}

Solution

  • The colon in the for loop is an example of Range-based for loop

    range_expression is evaluated to determine the sequence or range to iterate. Each element of the sequence, in turn, is dereferenced and assigned to the variable with the type and name given in range_declaration.

    Please check here for more information