-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Describe the bug
When given the least possible signed integer value, gcd cannot make it positive, and returns a negative result.
According to [numeric.ops.gcd]/2, it is UB:
Preconditions:
|m|and|n|are representable as a value ofcommon_type_t<M, N>.
[Note 1: These requirements ensure, for example, thatgcd(m, m)=|m|is representable as a value of typeM. — end note]
According to [expr.const]/6 whereas language UB shouldn't compile, it is implementation-specific if library precondition violation compiles in constexpr context or not.
Since we are permitted do a favor and reject overflowing value, we should.
The same is for lcm, except that it should also check the result.
Command-line test case
#include <limits>
#include <numeric>
using namespace std;
auto test() {
constexpr int x = numeric_limits<int>::lowest();
constexpr int y = numeric_limits<int>::lowest();
constexpr auto z = std::gcd(x, y);
return z;
}returns -1
Expected behavior
Don't compile
STL version
Version 17.0.0 Preview 6.0
Additional context
From this conversation https://discord.com/channels/737189251069771789/740318951766098012/902077925506039808