Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace std::bindX and std::mem_fun with lambdas. #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

FtZPetruska
Copy link

Also see pokemon-speedrunning/gambatte-core#18


std::bind1st, std::bind2nd, and std::mem_fun were deprecated in C++11 and removed in C++17.

GCC is the only compiler that allows using these functions regardless of the standard. Clang (both LLVM, and Apple Clang) and MSVC will error out on standard >=C++17. This is currently mainly an issue for LLVM Clang as starting LLVM 16, Clang defaults to C++17 and no standard used to be set in qmake.

This PR addresses this issue by using C++11 lambdas. This typically offers better performances compared to the alternative std::bind and std::mem_fn, and better extensibility if more methods need to be called in the same loop.

Benchmark for reference

Here are some speed results for calling the same member function 1'000'000'000 times:

Took 3.79699s to run 1000000000 steps with lambda
Took 4.25883s to run 1000000000 steps with mem_fn // Only for object pointers, no equivalent to mem_fun_ref
Took 13.6731s to run 1000000000 steps with bind
Took 16.3081s to run 1000000000 steps with mem_fn+bind

std::bind1st, std::bind2nd, and std::mem_fun were deprecated in C++11
and removed in C++17.

This is mainly an issue on LLVM 16 as C++17 was made the default.
MSVC also errors out on C++17 and above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant