title: Is numeric_limits<int>::is_modulo logically contradictory?
body: <p>In <a href="https://stackoverflow.com/questions/13150449/">another question</a>, the topic of <code>std::numeric_limits&lt;int&gt;::is_modulo</code> 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.</p>
  
  <p>Let me start with some code:</p>
  
  <pre><code>#include &lt;limits&gt;
  #include &lt;iostream&gt;
  
  bool test(int x)
  {
      return x+1 &gt; x;
  }
  
  int main(int argc, char *argv[])
  {
      int big = std::numeric_limits&lt;int&gt;::max();
  
      std::cout &lt;&lt; std::numeric_limits&lt;int&gt;::is_modulo &lt;&lt; " ";
      std::cout &lt;&lt; big+1 &lt;&lt; " ";
      std::cout &lt;&lt; test(big) &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>When I compile this with <code>g++ -O3 -std=c++11</code> (x86_64 GCC 4.7.2), it produces the following output:</p>
  
  <pre><code>1 -2147483648 1
  </code></pre>
  
  <p>That is, <code>is_modulo</code> is true, one plus <code>INT_MAX</code> is negative, and one plus <code>INT_MAX</code> is greater than <code>INT_MAX</code>.</p>
  
  <p>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 <code>x+1</code> cannot be <code>INT_MAX</code>; therefore the compiler may (and will) compile the <code>test</code> function to return <code>true</code> unconditionally.  So far, so good.</p>
  
  <p>However, the <a href="https://stackoverflow.com/questions/7238958/">C++11 spec</a> also says (18.3.2.4 paragraphs 60-61):</p>
  
  <blockquote>
    <p><strong><code>static constexpr is_modulo;</code></strong></p>
    
    <p>True if the type is modulo.222 A type is modulo if, for any operation
    involving <code>+</code>, <code>-</code>, or <code>*</code> on values of that type whose result would
    fall outside the range <code>[min(),max()]</code>, the value returned differs
    from the true value by an integer multiple of <code>max() - min() + 1</code>.</p>
    
    <p>On most machines, this is <code>false</code> for floating types, <code>true</code> for
    unsigned integers, and <code>true</code> for signed integers.</p>
  </blockquote>
  
  <p>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 <code>is_modulo == true</code> creating an exception.</p>
  
  <p>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 <code>is_modulo</code> as <code>true</code> even though signed arithmetic most certainly does not wrap around.</p>
  
  <p>Is the standard buggy?  Is GCC non-conforming?  Am I missing something?</p>