dimanche 2 avril 2017

Default parameter for template function with forwarding

I have a template function that looks like this:

template <typename SyncFunc>
void waitForConnected(int check_period_in_ms, SyncFunc&& syncFunc = std::function<void()>()) //this kind of default doesn't work
{
    auto established_future = connected_promise.get_future();
    while (established_future.wait_for(std::chrono::milliseconds(check_period_in_ms)) != std::future_status::ready)
    {
        syncFunc();
    }
    established_future.get();
}

I use it to provide an option for GUI synchronization to the user.

I would like to be able to call it with waitForConnected(10), i.e., without having to put the second parameter explicitly. Is there a way to do this other than function overloading? I mean just with a default parameter for the second argument that's basically an empty function: [](){}, such that the call automatically becomes waitForConnected(10,[](){})?

Aucun commentaire:

Enregistrer un commentaire