title: Dealing with size of stl containers
body: <p>I'm rewriting a general purpose library that was written by me before I've learned STL. It uses C-style arrays all the way. In many places there is a code like this:</p>
  
  <pre><code>unsigned short maxbuffersize; // Maximum possible size of the buffer. Can be set by user.
  unsigned short buffersize; // Current size of the buffer.
  T *buffer; // The buffer itself.
  </code></pre>
  
  <p>The first thing I did was to change the code like this:</p>
  
  <pre><code>unsigned short maxbuffersize;
  unsigned short buffersize;
  std::vector&lt;T&gt; buffer;
  </code></pre>
  
  <p>And then:</p>
  
  <pre><code>typedef unsigned short BufferSize;
  BufferSize maxbuffersize;
  BufferSize buffersize;
  std::vector&lt;T&gt; buffer;
  </code></pre>
  
  <p>And then I felt like I was doing a very bad thing and should reconsider my coding style. At first, BufferSize seemed like a very bad name for a type but then all kinds of weird questions started popping up. How do I name the size type? Should I use my own type or inherit from <code>std::vector&lt;T&gt;::size_type</code>? Should I cache the size of container or use <code>size()</code> all the way? Should I allow the user to manually set the maximum size of container and if not, how do I check for overflow?</p>
  
  <p>I know that there can't be one-size-fits-all approach therefore I'd like to hear the policies other coders and framework vendors use. The library I'm working on is cross-platform general purpose and is intended to be released into public domain and be used for decades. Thanks.</p>