What is the C ++ equivalent for C #

I am trying to avoid managed managed C ++ (CLI) overflow. There is an unchecked keyword in C #, and C ++ overflow doesn't end in exceptions.

For reference, unchecked is documented here . Basically if you do this:

unchecked
{
      int1 = 2147483647 + 10; //this overflows in CLI but is ok in C# and C++
}

      

In C # it will not overflow, but rather converts to int by taking the least significant bits. This is useful if, for example, you are calculating hash codes.

Note. I realize there is no equivalent C ++ keyword, but some bit shifting should do the trick;

+3


source to share


2 answers


You can just use #pragma unmanaged

around the method to get normal uncontrolled behavior in C ++.



+3


source


Use unsigned data type for bit operations and checksums. The workaround for unsigned behavior is well defined in C ++.



+2


source







All Articles