title: Why is unsigned short (multiply) unsigned short converted to signed int?
body: <p>Why is <code>unsigned short * unsigned short</code> converted to <code>int</code> in C++11?</p>
  <p>The <code>int</code> is too small to handle max values as demonstrated by this line of code.</p>
  <pre><code>cout &lt;&lt; USHRT_MAX * USHRT_MAX &lt;&lt; endl;
  </code></pre>
  <p>overflows on MinGW 4.9.2</p>
  <pre><code>-131071
  </code></pre>
  <p>because (<a href="http://www.cplusplus.com/reference/climits/" rel="nofollow noreferrer">source</a>)</p>
  <blockquote>
  <p>USHRT_MAX = 65535 (2^16-1) or greater*</p>
  <p>INT_MAX = 32767 (2^15-1) or greater*</p>
  </blockquote>
  <p>and <code>(2^16-1)*(2^16-1) = ~2^32</code>.</p>
  <hr />
  <p>Should I expect any problems with this <strong>solution</strong>?</p>
  <pre><code>unsigned u = static_cast&lt;unsigned&gt;(t*t);
  </code></pre>
  <hr />
  <p>This program</p>
  <pre><code>unsigned short t;
  cout&lt;&lt;typeid(t).name()&lt;&lt;endl;
  cout&lt;&lt;typeid(t*t).name()&lt;&lt;endl;
  </code></pre>
  <p>gives output</p>
  <pre><code>t
  i
  </code></pre>
  <p>on</p>
  <pre><code>gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
  gcc version 4.8.2 (GCC)
  MinGW 4.9.2
  </code></pre>
  <p>with both</p>
  <pre><code>g++ p.cpp
  g++ -std=c++11 p.cpp
  </code></pre>
  <p>which proves that <code>t*t</code> is converted to <code>int</code> on these compilers.</p>
  <hr />
  <h2>Usefull resources:</h2>
  <p><a href="https://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe">Signed to unsigned conversion in C - is it always safe?</a></p>
  <p><a href="https://stackoverflow.com/questions/16966636/signed-unsigned-integer-multiplication">Signed &amp; unsigned integer multiplication</a></p>
  <p><a href="https://bytes.com/topic/c-sharp/answers/223883-multiplication-types-smaller-than-int-yields-int" rel="nofollow noreferrer">https://bytes.com/topic/c-sharp/answers/223883-multiplication-types-smaller-than-int-yields-int</a></p>
  <p><a href="http://www.cplusplus.com/reference/climits" rel="nofollow noreferrer">http://www.cplusplus.com/reference/climits</a></p>
  <p><a href="http://en.cppreference.com/w/cpp/language/types" rel="nofollow noreferrer">http://en.cppreference.com/w/cpp/language/types</a></p>
  <hr />
  <p><strong>Edit:</strong> I have demonstrated the problem on the following image.</p>
  <p><a href="https://i.stack.imgur.com/zoKr4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zoKr4.png" alt="enter image description here" /></a></p>