How do I safely cast between any numeric types with clipping if required?
<p>I'd like a function of the following form</p>

<pre><code>template &lt;typename T, typename U&gt;
U clipAndCast(T x)
{
  ... 
  return y;  
 }, 
</code></pre>

<p>where y is x cast to type U, but with x clipped if required so that this cast is well-defined.</p>

<p>Is there a library or boost function do to this? I can't find one, so if not what's the best way to write such a function?</p>
