Skip to main content
Code Review

Return to Question

replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

In a recent discussion about a question question on another Stack Exchange site, I was made aware that seemingly innocent arithmetic expressions of unsigned integral types in C++ can easily invoke undefined behavior.

In a recent discussion about a question on another Stack Exchange site, I was made aware that seemingly innocent arithmetic expressions of unsigned integral types in C++ can easily invoke undefined behavior.

In a recent discussion about a question on another Stack Exchange site, I was made aware that seemingly innocent arithmetic expressions of unsigned integral types in C++ can easily invoke undefined behavior.

Notice removed Draw attention by 5gon12eder
Bounty Ended with user105959's answer chosen by 5gon12eder
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
added tl;dr
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29

tl;dr The purpose of the code is to allow writing

constexpr auto x = std::uint16_t { UINT16_MAX };
constexpr auto y = std::uint16_t { UINT16_MAX };
static_assert(multiply(x, y) == 1u, "broken math");

in a portable way that won't invoke undefined behavior. If that is enough for you to understand the problem at hand, you are welcome to skip the first two sections and jump straight to the section titled "Code for Review".

Otherwise, the section titled "Problem Statement" explains why simply writing x * y would not have done the trick and the section "Proposed Solution" provides background information about the implementation of the multiply function I'm asking review for.

tl;dr The purpose of the code is to allow writing

constexpr auto x = std::uint16_t { UINT16_MAX };
constexpr auto y = std::uint16_t { UINT16_MAX };
static_assert(multiply(x, y) == 1u, "broken math");

in a portable way that won't invoke undefined behavior. If that is enough for you to understand the problem at hand, you are welcome to skip the first two sections and jump straight to the section titled "Code for Review".

Otherwise, the section titled "Problem Statement" explains why simply writing x * y would not have done the trick and the section "Proposed Solution" provides background information about the implementation of the multiply function I'm asking review for.

Notice added Draw attention by 5gon12eder
Bounty Started worth 50 reputation by 5gon12eder
Tweeted twitter.com/StackCodeReview/status/730942119599308801
added 79 characters in body
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29
Loading
added 225 characters in body
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29
Loading
added 5 characters in body
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29
Loading
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /