title: C++ vector::size_type: signed vs unsigned; int vs. long
body: <p>I have been doing some testing of my application by compiling it on different platforms, and the shift from a 64-bit system to a 32-bit system is exposing a number of issues.</p>
  
  <p>I make heavy use of vectors, strings, etc., and as such need to count them.  However, my functions also make use of 32-bit unsigned numbers because in many cases I need to explicitly consume a positive integer.  </p>
  
  <p>I'm having issues with seemingly simple tasks such as <code>std::min</code> and <code>std::max</code>, which may be more systemic.  Consider the following code:</p>
  
  <pre><code>uint32_t getmax()
  {
      return _vecContainer.size();
  }
  </code></pre>
  
  <p>Seems simple enough: I know that a vector can't have a negative number of elements, so returning an unsigned integer makes complete sense.</p>
  
  <pre><code>void setRowCol(const uint32_t &amp;r_row; const uint32_t &amp;r_col)
  {
      myContainer_t mc;
      mc.row = r_row;
      mc.col = r_col;
      _vecContainer.push_back(mc);
  }
  </code></pre>
  
  <p>Again, simple enough.</p>
  
  <p>Problem:</p>
  
  <pre><code>uint32_t foo(const uint32_t &amp;r_row)
  {
      return std::min(r_row, _vecContainer.size());
  }
  </code></pre>
  
  <p>This gives me errors such as:</p>
  
  <pre><code>/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:2589:1: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('unsigned long' vs. 'unsigned int')
  min(const _Tp&amp; __a, const _Tp&amp; __b)
  </code></pre>
  
  <p>I did a lot of digging, and on one platform vector::size_type is an 8 byte number.  However, by design I am using unsigned 4-byte numbers.  This is presumably causing things to be wacky because you cannot implicitly convert from an 8-byte number to a 4-byte number.</p>
  
  <p>The solution was to do this the old fashioned weay:</p>
  
  <pre><code>#define MIN_M(a,b) a &lt; b ? a : b
  return MIN_M(r_row, _vecContainer.size());
  </code></pre>
  
  <p>Which works dandy.  But the systemic issue remains: when planning for multiple platform support, how do you handle instances like this?  I could use size_t as my standard size, but that adds other complications (e.g. moving from one platform which supports 64 bit numbers to another which supports 32 bit numbers at a later date).  The bigger issue is that size_t is unsigned, so I can't update my signatures:</p>
  
  <pre><code>size_t foo(const size_t &amp;r_row)
  // bad, this allows -1 to be passed, which I don't want
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>EDIT: I had read somewhere that size_t was signed, and I've since been corrected.  So far it looks like this is a limitation of my own design (e.g. 32-bit numbers vs. using std::vector::size_type and/or size_t). </p>