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

Support constexpr in min and max #251

Merged
merged 2 commits into from
Jun 24, 2024
Merged

Conversation

chiphogg
Copy link
Contributor

These have been constexpr compatible since C++14. Mechanically, the
way I did this was:

  1. Find an overload that was not constexpr.
  2. Make a test that requires it to be constexpr to pass (either
    tweaking an existing test, or making a new one).
  3. Make sure the test fails.
  4. Change the function until the test passes.

The biggest challenge was that our default compiler wouldn't accept our
lambda in a constexpr context. That feels like a bug, but in any
case, we were able to work around it by making a manual function object
with an explicitly constexpr call operator.

Fixes #244.

These have been `constexpr` compatible since C++14.  Mechanically, the
way I did this was:

1. Find an overload that was not `constexpr`.
2. Make a test that requires it to be `constexpr` to pass (either
   tweaking an existing test, or making a new one).
3. Make sure the test fails.
4. Change the function until the test passes.

The biggest challenge was that our default compiler wouldn't accept our
lambda in a `constexpr` context.  That feels like a bug, but in any
case, we were able to work around it by making a manual function object
with an explicitly `constexpr` call operator.

Fixes #244.
@chiphogg chiphogg added the release notes: ✨ lib (enhancement) PR enhancing the library code label Jun 23, 2024
@chiphogg chiphogg requested a review from geoffviola June 23, 2024 20:57
Copy link
Contributor

@geoffviola geoffviola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes make sense. It looks like lambdas in a constexpr context have been added in C++17: https://stackoverflow.com/a/32697323

@chiphogg
Copy link
Contributor Author

Well... that would sure explain why it didn't work before! TIL.

@chiphogg chiphogg merged commit 32ae57d into main Jun 24, 2024
10 checks passed
@chiphogg chiphogg deleted the chiphogg/constexpr-min-max#244 branch June 24, 2024 15:28
Copy link
Contributor

@hoffbrinkle hoffbrinkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, thanks!

@@ -318,31 +318,41 @@ constexpr bool isnan(QuantityPoint<U, R> p) {
return std::isnan(p.in(U{}));
}

namespace detail {
// Some compilers do not support lambdas in constexpr contexts, so we make a manual function object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the comment about this being a C++17 thing, or is it the case that even with C++17, some compilers still don't support it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Addressed in #252.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: ✨ lib (enhancement) PR enhancing the library code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

min and max functions are not constexpr
3 participants