gpt4 book ai didi

c++ - C++ 标准是否允许添加两个整数(基本类型 int)来引发 C++ 异常?

转载 作者:IT老高 更新时间:2023-10-28 22:33:23 25 4
gpt4 key购买 nike

标准是否允许这样做?

我认为不会。有人会。我需要聪明的人来证明他是错的。

最佳答案

是的,确实如此。有符号整数的溢出是未定义的行为,因此任何事情都可能发生(包括抛出异常)。

作为旁注,我必须说这对于大多数实现来说不太可能发生(但绝对可能);正如 C++11 标准在第 5/4 段的注释中提到的那样:

If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. [ Note: most existing implementations of C++ ignore integer overflows. Treatment of division by zero, forming a remainder using a zero divisor, and all floating point exceptions vary among machines, and is usually adjustable by a library function. —end note ]

As hvd mentions in the comments ,但是,一些实现允许为整数溢出提供自定义处理程序,并且这些处理程序可能会抛出。

关于c++ - C++ 标准是否允许添加两个整数(基本类型 int)来引发 C++ 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16636073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com