title: C++ implicit numeric type demoting
body: <p>Recently, I have noticed that C/C++ seems to be very permissible with numeric type conversion, as it implicitly casts a double to int.</p>
  
  <p><strong>Test:</strong></p>
  
  <p>Environment: <code>cpp.sh</code>, <code>Standard C++ 14</code>, <code>Compilation warnings all set</code></p>
  
  <p>Code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int intForcingFunc(double d) {
      return d;                       // this is allowed
  }
  
  int main() {
      double d = 3.1415;
      double result = intForcingFunc(d);
      printf("intForcingFunc result = %f\n", result);
  
      int localRes = d;               // this is allowed
      printf("Local result = %d\n", localRes);
  
      int staticCastRes = static_cast&lt;int&gt;(d);                // also allowed
      printf("Static cast result = %d\n", staticCastRes);
  }
  </code></pre>
  
  <p>No warnings are issues during compilation.</p>
  
  <p><a href="http://www.cplusplus.com/doc/tutorial/typecasting/" rel="nofollow noreferrer">Documentation</a> mentions tangentially the subject, but misses the exact case of the question:</p>
  
  <blockquote>
    <p>C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion, known in C++ as type-casting.</p>
  </blockquote>
  
  <p>I have also tried in a managed language (C#) and all these cases are not allowed (as expected):</p>
  
  <pre class="lang-cs prettyprint-override"><code>static int intForcingFunc(double d)
  {
      // Not legal: Cannot implicitly convert type 'double' to 'int'
      // return d;
      return Convert.ToInt32(d);
  }
  
  static void Main(string[] args)
  {
      double d = 3.1415;
      double result = intForcingFunc(d);
      Console.WriteLine("intForcingFunc result = " + result);
  
      // Not legal: Cannot implicitly convert type 'double' to 'int'
      // int localRes = d;
      int localRes = (int)d;
      Console.WriteLine("local result = " + result);
  
      Console.ReadLine();
  }
  </code></pre>
  
  <p>Why is this behavior allowed in a strong-typed language? In most cases, this is undesired behavior. One reason behind this seems to be lack of <a href="https://stackoverflow.com/questions/15014214/c-cli-equivalent-of-c-sharp-checked-keyword">arithmetic overflow detection</a>.</p>