std::move vs std::forward
Pass-by-value and std::move
is suggested by clang-tidy whenever your function is just a wrapper. And that’s why I used this idiom in the pool_type playground repository. But during the last meetup came out that I can improve it, at least at some extent, by leveraging the more generic std::forward
instead of std::move
. That’s why, in the following code, both Connection
and Listener
are template alias:
Connection on(Listener f) noexcept {
return onL.emplace(onL.cend(), std::move(f));
}
So std::forward
can be leveraged yielding the following code:
Connection on(Listener&& f) noexcept {
return onL.emplace(onL.cend(), std::forward<Listener>(f));
}
This is an improvement in performance (verified by godbolt). But I’m not sure about the lifetime safety. I guess if someone is using this pool_type template class passing to the ::on
a local instantiation of a std::function
, it will simply segfault.
I will talk with the maintainer of uvw to understand if this improvement can be ported on the upstream repo too.
Reference
1) https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers
2) (https://github.com/stefanofiorentino/pool_type.git
3) https://www.meetup.com/lugano-c-meetup/events/283927602/
4) https://godbolt.org/