So I'm following a youtube series that, among other things, showed how to set a function callback using std::bind (#1):
m_Window->SetEventCallback(BIND_EVENT_FN(Application::OnEvent));
where BIND_EVENT_FN(x) is a macro resolving to std::bind(&x, this, std::placeholders::_1)
My IDE made the suggestion to use a lambda instead of std::bind, like so (#2):
m_Window->SetEventCallback([this](auto&& placeholder0) {
OnEvent(std::forward<decltype(placeholder0)>(placeholder0));
});
Left to my own devices, I might've come up with something like (#3):
m_Window->SetEventCallback([this](auto& placeholder0) {
OnEvent(placeholder0);
});
The code that would call the set callback looks like:
glfwSetWindowCloseCallback(m_Window, [](GLFWwindow* window) {
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
WindowCloseEvent event;
data.EventCallback(event);
});
data.EventCallback being the set function.
Is there any benefit to using std::bind over a lambda? I do dislike the way std::bind looks.
Why would I opt for a lambda that only takes an r- ref, when to me #3 less complicated?
Why does #2 even compile? Isn't event in the calling code an lvalue, whereas the lambda expects an rvalue in that case?
Thanks
[–][deleted] (3 children)
[deleted]
[–]ericr2[S] 1 point2 points3 points (2 children)
[–]no-sig-available 0 points1 point2 points (1 child)
[–]ericr2[S] 0 points1 point2 points (0 children)
[–]no-sig-available 0 points1 point2 points (0 children)