Skip to content

Conversation

w0xlt
Copy link
Contributor

@w0xlt w0xlt commented May 15, 2025

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 standard std::integral for clearer intent.

  • Annotation with [[nodiscard]] and noexcept. Compilers will warn if the return value is discarded and since std::from_chars is guaranteed to throw nothing, declaring the wrapper noexcept makes the exception guarantee explicit.

  • With C++23, the integral overloads of std::from_chars are constexpr, allowing ToIntegral to become a constexpr function.

https://en.cppreference.com/w/cpp/utility/from_chars

@DrahtBot
Copy link
Contributor

DrahtBot commented May 15, 2025

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32522.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK shahsb

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

Copy link

@shahsb shahsb left a 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

Copy link

@shahsb shahsb left a 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.

Copy link

@shahsb shahsb left a 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{};
Copy link
Member

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.

Copy link
Member

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.)

Copy link
Member

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?

Comment on lines +175 to +176
* @tparam T Integral type (excluding bool).
* @param str The input view to parse.
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants