-
Notifications
You must be signed in to change notification settings - Fork 37.7k
util: C++20 ToIntegral()
Improvement
#32522
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
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32522. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsNo conflicts as of last run. |
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.
Concept ACK
Approach ACK
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.
LGTM. Indeed, good improvement.
Thanks for making the changes.
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 6135cc3
{ | ||
static_assert(std::is_integral_v<T>); | ||
T result; | ||
T result{}; |
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.
not sure. We actually want C++26 erroneous behavior here, not [[indeterminate]]
, nor (zero-)initialized. Otherwise, it will become harder to diagnose bugs in the code with the compiler or with sanitizers.
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.
(realistically speaking in this very instance it probably doesn't make a difference and anything is fine, I just wanted to mention it for the general context.)
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.
Otherwise, it will become harder to diagnose bugs in the code with the compiler or with sanitizers.
@dergoegge any opinion?
* @tparam T Integral type (excluding bool). | ||
* @param str The input view to parse. |
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.
not sure about adding docs that are already self-explanatory from the code without doubt. Adding such docs just means that two lines will have to be modified when modifying a trivial single line of code. Also, if the docs aren't needed, they are only a source of inconsistencies and typos, etc.
While reviewing #32520, I noticed that the function
ToIntegral()
could be improved:Instead of manually asserting
std::is_integral_v<T>
, the template can be constrained with the standardstd::integral
for clearer intent.Annotation with
[[nodiscard]]
andnoexcept
. Compilers will warn if the return value is discarded and sincestd::from_chars
is guaranteed to throw nothing, declaring the wrappernoexcept
makes the exception guarantee explicit.With C++23, the integral overloads of
std::from_chars
areconstexpr
, allowingToIntegral
to become aconstexpr
function.https://en.cppreference.com/w/cpp/utility/from_chars