title: Comparison size_t variable with -1 (maximum size value) in c++ code
body: <p>I'm refactoring a library and trying to get rid of many gcc warnings. The big part of these warning are about signed / unsigned comparison and are related to the usage of <code>size_t</code>. The library works on 64 bit Linux systems.</p>
  
  <p>A programmer used -1 as the special value similar to <code>std::string::npos</code>.
  There are many places in the library where code looks like this:</p>
  
  <pre><code>class AnnotationBase
  {
      public:
          size_t m_offset = -1;
          size_t m_length = -1;
  
  }
  
  ...
  
  AnnotationBase foo(const std::string&amp; text, const AnnotationBase&amp; annot)
  {
      AnnotationBase newAnnot;
  
      // do some job with annotations and text
      ...
  
      if(newAnnot.m_offset == -1)
      {
          // do some other job
          ...
      }
  
      return newAnnot;
  }
  </code></pre>
  
  <p>The problem lies in the warning that gcc generates on the line <code>if(newAnnot.m_offset == -1)</code> because of a signed / unsigned comparison:</p>
  
  <pre><code>"warning: comparison between signed and unsigned integer expressions [-Wsign-compare]"
  </code></pre>
  
  <p>What is the proper way to compare <code>size_t</code> variable in C++ with the maximum value (-1) without warning? Doing this like <code>if(newAnnot.m_offset == std::numeric_limits&lt;size_t&gt;::max())</code> is very inconvenient due to complexity and length of this expression.</p>
  
  <p>Is it a good way to use C-style defined value <code>SIZE_MAX</code> or it is better to create own constant like <code>namesapce libling { const NONE = std::numeric_limits&lt;size_t&gt;::max(); }</code> (Creating new constant leads to many similar constants in different libraries and namespaces like <code>libling::NONE</code>, <code>libother::UNKNOWN</code>, <code>liblongnamesapcename::NOTHING</code>)? </p>