title: What's an efficient way to avoid integer overflow converting an unsigned int to int in C++?
body: <p>Is the following an efficient and problem free way to convert an unsigned int to an int in C++:</p>
  
  <pre><code>#include &lt;limits.h&gt;
  void safeConvert(unsigned int passed) 
  {
      int variable = static_cast&lt;int&gt;(passed % (INT_MAX+1)); 
      ...
  }
  </code></pre>
  
  <p>Or is there a better way? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p><strong>As pointed out by James McNellis it is not undefined to assign an unsigned int > INT_MAX to an integer - rather this is implementation defined. As such the context here is now specifically on my preference is to ensure this integer resets to zero when the unsigned int exceeds INT_MAX.</strong></p>
  
  <p><em>Original Context</em></p>
  
  <p>I have a number of unsigned int's used as counters, but want to pass them around as integers in a specific case.  </p>
  
  <p>Under normal operation these counts will remain within the bounds of INT_MAX. However to avoid running into <strike>undefined</strike> implementation specific behaviour should the abnormal (but valid) case occur I want some efficient conversion here. </p>