title: Writing std::min/std::max for ints of heterogenous widths
body: <p>My C++ program uses unsigned ints of different widths to express constraints on what data can be represented. For example, I have a file whose size is a <code>uint64_t</code>, and I wish to read it in chunks with a buffer whose size is a <code>size_t</code>. A chunk is the smaller of the buffer size and the (remaining) file size:</p>
  
  <pre><code>uint64_t file_size = ...;
  size_t buffer_size = ...;
  size_t chunk_size = std::min(buffer_size, file_size);
  </code></pre>
  
  <p>but this fails because <code>std::min</code> requires that both parameters have the same type, so I must cast up and then back down:</p>
  
  <pre><code>size_t chunk_size = \
      static_cast&lt;size_t&gt;(std::min(static_cast&lt;uint64_t&gt;)buffer_size, \
                                   file_size));
  </code></pre>
  
  <p>This casting ought to be unnecessary, because it is obvious that <code>min(size_t, uint64_t)</code> will always fit in a <code>size_t</code>.</p>
  
  <p>How can I write a generic <code>min</code> function that takes two (possibly different) unsigned types, and whose return type is the smaller of the two types?</p>