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
util: Add SaturatingAdd helper #24224
Conversation
Concept ACK. This seems useful, hopefully C++ will have built-ins for explicitly saturating/wrapping/checked arithmetic like rust some day. But until then… |
I wasn't aware that rust has those built-in. Mind sharing a link to the docs? |
Oh, I see. By default rust will panic, but there are https://doc.rust-lang.org/std/num/index.html: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review ACK fa90189
@@ -28,4 +28,22 @@ template <class T> | |||
return i + j; | |||
} | |||
|
|||
template <class T> | |||
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, any reason you didn't constexpr
this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outside of unit tests I don't think this will ever be called in a context that would be constexpr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK fa90189
Added a test. Should be trivial to re-ACK with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK faa7d8a
reACK faa7d8a |
Seems good to have this in the repo, as it might be needed to write cleaner code. For example: