1

I can't help but notice that all people writing templates for functions use rvalue references. I'm not sure I'm right, but it's a tendency I noticed. Here's my simple example:

template <typename SyncFunc>
void waitForEstablished(int check_period_in_ms, SyncFunc&& syncFunc)
{
    auto established_future = promise_established.get_future();
    while (established_future.wait_for(std::chrono::milliseconds(check_period_in_ms)) != std::future_status::ready)
    {
        syncFunc();
    }
    established_future.get();
}

Explanation of this function: In this function, I provide a waiting method for the user, and provide a mechanism for synchronization/refreshing of a GUI. It helps in separating concerns. The user provides the function that should be called every check_period_in_ms milliseconds to prevent things from blocking. This is useful for the case when in a GUI application, the following is how I call this:

commThreadController.waitForEstablished(10,[this](){QApplication::processEvents();});

The function QApplication::processEvents() processes the GUI event loop.

But I'm really puzzled on what kind of reference I should use (always rvalue? when should it be something else?) and whether it makes a difference in any extreme case.

Should I be passing all function templates by rvalue reference?

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189

0 Answers0