什么是C ++的C ++等价物 [英] What is the C++ equivalent of the C# checked

查看:171
本文介绍了什么是C ++的C ++等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望避免托管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 ++等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆