title: C++ functions for integer division with well defined rounding strategy
body: <p>I want something in C++ that lets me do efficient integer division with specified rounding behavior, something like this:</p>
  
  <pre><code>div_down(-4,3)        ==&gt; -2
  div_up(4,3)           ==&gt; 2
  div_to_zero(-4,3)     ==&gt; -1
  div_to_nearest(5,3)   ==&gt; 2
  </code></pre>
  
  <p>I'd like it to detect target machine behavior at compile time and generate the appropriate optimal implementations. Something similar for modulus would also be nice, abstracting out the undefined behavior for negative operands at compile time.</p>
  
  <p>Does this exist?</p>
  
  <p>If not, what's a nice way to make it? I can think of a few possible approaches:</p>
  
  <ul>
  <li>Try to implement them as single expressions that statically optimize</li>
  <li>Use constant expressions to detect target behavior and choose from multiple implementations, parhaps using templates (but how exactly?)</li>
  </ul>