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

Add cmp::min cmp::max #115

Merged
merged 1 commit into from
Aug 8, 2023
Merged

Add cmp::min cmp::max #115

merged 1 commit into from
Aug 8, 2023

Conversation

tcbrindle
Copy link
Owner

Because they're ordinary function templates, std::min and std::max can't be passed as arguments to functions without wrapping them in lambdas (or doing a horrible function pointer cast). This makes me sad.

std::ranges::min and std::ranges::max are function objects and so can be passed as function arguments -- except with MSVC, which annoyingly goes out of its way to prevent you doing this very useful thing. This also makes me sad.

To improve matters, we'll add flux::cmp::min and flux::cmp::max which take two arguments and an optional comparator and return the lesser and greater respectively.

As an added bonus, max() now correctly returns the second argument if both are equal, and our versions of these functions should be less likely than the standard versions to cause dangling when used with rvalues.

Because they're ordinary function templates, `std::min` and `std::max` can't be passed as arguments to functions without wrapping them in lambdas (or doing a horrible function pointer cast). This makes me sad.

`std::ranges::min` and `std::ranges::max` are function objects and so can be passed as function arguments -- except for MSVC, which annoyingly goes out of its way to prevent you doing this very useful thing. This also makes me sad.

To improve matters, we'll add `flux::cmp::min` and `flux::cmp::max` which take two arguments and an optional comparator and return the lesser and greater respectively.

As an added bonus, `max()` now correctly returns the second argument if both are equal, and our versions of these functions should be less likely than the standard versions to cause dangling when used with rvalues.
@codecov
Copy link

codecov bot commented Aug 8, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (e83bdcb) 97.58% compared to head (44a3078) 97.58%.
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #115   +/-   ##
=======================================
  Coverage   97.58%   97.58%           
=======================================
  Files          66       66           
  Lines        2276     2280    +4     
=======================================
+ Hits         2221     2225    +4     
  Misses         55       55           
Files Changed Coverage Δ
include/flux/core/functional.hpp 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tcbrindle tcbrindle merged commit 8048d83 into main Aug 8, 2023
@tcbrindle tcbrindle deleted the pr/cmp_min_max branch August 9, 2023 12:19
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