问题描述
我希望避免托管C ++(CLI)中的溢出。在C#中有一个未检查的关键字,并且在C ++溢出中不会在异常中结束。
为了引用,未检查的记录。基本上如果你这样做:
取消选中
{
int1 = 2147483647 + 10; //在C#和C ++中溢出,但在C#和C ++中溢出
}
不会溢出,但通过取最低有效位转换为int。注意:我意识到没有等价的C ++关键字,但是一些位移应该做的伎俩;
您可以在方法周围使用 #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++.
这篇关于什么是C ++的C ++等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!