且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是C ++的C ++等价物

更新时间:2022-10-15 08:49:14

您可以在方法周围使用 #pragma unmanaged 在本机C ++中。


I'm looking to avoid an overflow in managed C++ (CLI). In C# there is an unchecked keyword, and in C++ overflows do not end up in exceptions.

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

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

In C# it will not overflow but convert to int by taking the least significant bits. This is appropriate when you compute hash codes for example.

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

You can just use #pragma unmanaged around a method to get the normal unchecked behavior in native C++.