title: Could this C cast to avoid a signed/unsigned comparison make any sense?
body: <p>I'm reviewing a C++ project and see effectively the following:</p>
  
  <pre><code>std::vector&lt;SomeType&gt; objects;
  
  //then later
  int size = (int)objects.size();
  for( int i = 0; i &lt; size; ++i ) {
      process( objects[i] );
  }
  </code></pre>
  
  <p>Here's what I see. <code>std::vector::size()</code> returns <code>size_t</code> that can be of some size not related to the size of <code>int</code>. Even if <code>sizeof(int) == sizeof(size_t)</code> <code>int</code> is signed and can't hold all possible values of <code>size_t</code>. So the code above could only process the lower part of a very long vector and contains a bug. The right way would be to use <code>size_t</code> for both the <code>size</code> variable and the loop index.</p>
  
  <p>That said I'm curious of why the author might have written this?</p>
  
  <p>My only guess is that first he omitted the <code>(int)</code> cast and the compiler emitted something like Visual C++ <a href="http://msdn.microsoft.com/en-us/library/y92ktdf2(VS.71).aspx" rel="nofollow noreferrer">C4018</a> warning:</p>
  
  <pre><code>warning C4018: '&lt;' : signed/unsigned mismatch
  </code></pre>
  
  <p>so the author though that the best way to avoid the compiler warning would be to simply cast the <code>size_t</code> to <code>int</code> thus making the compiler shut up.</p>
  
  <p>Is there any other possible sane reason for that C cast?</p>