Why can't the compiler conclude that a large number is long?

It compiles

var fourGb = (long)4*1024*1024*1024;

But it fails

var fourGb = 4*1024*1024*1024;

C "Overflow of operation during compilation in check mode."

So, if the compiler knows that this will be an overflow, why not conclude that the type of the variable should be long?

+3
source share
4 answers

Imagine the noise that can cause. "But the compiler can understand that an expression has to be evaluated for so long, why can't it?"

And that won't happen, too expensive.

, , . , . , .

+2

. http://msdn.microsoft.com/en-us/library/ctetwysk%28VS.80%29.aspx

int, int #. L, .

var fourGb = 4L * 1024 * 1024 * 1024;
+4

, , .

+2

:

  • "var"

Perhaps you have:

0
source

Source: https://habr.com/ru/post/1755659/


All Articles