In another question, the topic of std::numeric_limits<int>::is_modulo
came up. But the more I think about it, the more it seems like something is wrong with the spec or with GCC or both.
Let me start with some code:
#include <limits>
#include <iostream>
bool test(int x)
{
return x+1 > x;
}
int main(int argc, char *argv[])
{
int big = std::numeric_limits<int>::max();
std::cout << std::numeric_limits<int>::is_modulo << " ";
std::cout << big+1 << " ";
std::cout << test(big) << "
";
}
When I compile this with g++ -O3 -std=c++11
(x86_64 GCC 4.7.2), it produces the following output:
1 -2147483648 1
That is, is_modulo
is true, one plus INT_MAX
is negative, and one plus INT_MAX
is greater than INT_MAX
.
If you are the sort of person with any realistic chance of answering this question, you already know what happened here. The C++ specification says that integer overflow is Undefined Behavior; the compiler is allowed to assume you do not do that; therefore the argument to x+1
cannot be INT_MAX
; therefore the compiler may (and will) compile the test
function to return true
unconditionally. So far, so good.
However, the C++11 spec also says (18.3.2.4 paragraphs 60-61):
static constexpr is_modulo;
True if the type is modulo.222 A type is modulo if, for any operation
involving +
, -
, or *
on values of that type whose result would
fall outside the range [min(),max()]
, the value returned differs
from the true value by an integer multiple of max() - min() + 1
.
On most machines, this is false
for floating types, true
for
unsigned integers, and true
for signed integers.
Note that section 5 paragraph (4) still reads, "If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined." There is no mention of is_modulo == true
creating an exception.
So it looks to me like the standard is logically contradictory, because integer overflow cannot simultaneously be defined and undefined. Or at the very least, GCC is non-conforming, because it has is_modulo
as true
even though signed arithmetic most certainly does not wrap around.
Is the standard buggy? Is GCC non-conforming? Am I missing something?
See Question&Answers more detail:
os