| 1 | // { dg-do compile { target c++17 } } |
| 2 | #include <numeric> |
| 3 | #include <climits> |
| 4 | |
| 5 | // PR libstdc++/105844 |
| 6 | |
| 7 | // |INT_MIN| can be represented in common_type_t<int, unsigned> i.e. unsigned. |
| 8 | static_assert( std::lcm(INT_MIN, 1u) == INT_MAX+1u ); |
| 9 | static_assert( std::lcm(1u, INT_MIN) == INT_MAX+1u ); |
| 10 | |
| 11 | // But |INT_MIN| cannot be represented in common_type<int, int> i.e. int. |
| 12 | constexpr int a = std::lcm(INT_MIN, 1); // { dg-error "in .constexpr." } |
| 13 | constexpr int b = std::lcm(1, INT_MIN); // { dg-error "in .constexpr." } |
| 14 | |
| 15 | // And the LCM of 50000 and 49999 cannot be represented in int. |
| 16 | constexpr int c = std::lcm(50000, 49999); // { dg-error "in .constexpr." } |
| 17 | constexpr int d = std::lcm(49999, 50000); // { dg-error "in .constexpr." } |
| 18 | |
| 19 | // Similarly for unsigned, but the diagnostic is a failed assertion instead. |
| 20 | constexpr int e = std::lcm(500000u, 499999); // { dg-error "in .constexpr." } |
| 21 | constexpr int f = std::lcm(499999u, 500000); // { dg-error "in .constexpr." } |
| 22 | |
| 23 | // { dg-error "overflow" "" { target *-*-* } 0 } |
| 24 | // { dg-error "assert_fail" "" { target *-*-* } 0 } |