title: Is it safe to compare an unsigned int with a std::string::size_type
body: <p>I am going trough the book "Accelerated C++" by Andrew Koenig and Barbara E. Moo and I have some questions about the main example in chap 2. The code can be summarized as below, and is compiling without warning/error with g++:</p>
  
  <pre><code>#include &lt;string&gt;
  using std::string;
  
  int main()
  {
      const string greeting = "Hello, world!";
      // OK
      const int pad = 1;
      // KO
      // int pad = 1;
      // OK
      // unsigned int pad = 1;
      const string::size_type cols = greeting.size() + 2 + pad * 2;
      string::size_type c = 0;
      if (c == 1 + pad)
      {;}
  
      return 0;
  }
  </code></pre>
  
  <p>However, if I replace <code>const int pad = 1;</code> by <code>int pad = 1;</code>, the g++ compiler will return a warning:</p>
  
  <pre><code>warning: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
      if (c == 1 + pad)
  </code></pre>
  
  <p>If I replace <code>const int pad = 1;</code> by <code>unsigned int pad = 1;</code>, the g++ compiler will not return a warning.</p>
  
  <p>I understand why g++ return the warning, but I am not sure about the three below points:</p>
  
  <ul>
  <li>Is it safe to use an <code>unsigned int</code> in order to compare with a <code>std::string::size_type</code>? The compiler does not return a warning in that case but I am not sure if it is safe.</li>
  <li>Why is the compiler not giving a warning with the original code <code>const int pad = 1</code>. Is the compiler automatically converting the variable <code>pad</code> to an <code>unsigned int</code>?</li>
  <li>I could also replace <code>const int pad = 1;</code> by <code>string::size_type pad = 1;</code>, but the meaning of the variable <code>pad</code> is not really linked to a string size in my opinion. Still, would this be the best approach in that case to avoid having different types in the comparison?</li>
  </ul>