简体   繁体   English

C ++标准对于在目标类型范围之外的类型的转换结果的结果有何评价?

[英]What does the C++ standard say about results of casting value of a type that lies outside the range of the target type?

Recently I had to perform some data type conversions from float to 16 bit integer. 最近我不得不执行从float到16位整数的一些数据类型转换。 Essentially my code reduces to the following 基本上我的代码简化为以下内容

float f_val = 99999.0;
short int si_val = static_cast<short int>(f_val);

// si_val is now -32768

This input value was a problem and in my code I had neglected to check the limits of the float value so I can see my fault, but it made me wonder about the exact rules of the language when one has to do this kind of ungainly cast. 这个输入值是一个问题,在我的代码中,我忽略了检查浮点值的限制,所以我可以看到我的错,但它让我想知道语言的确切规则,当一个人必须做这种笨拙的演员。 I was slightly surprised to find that value of the cast was -32768. 我有点惊讶地发现演员的价值是-32768。 Furthermore, this is the value I get whenever the value of the float exceeds the limits of a 16 bit integer. 此外,只要float的值超过16位整数的限制,这就是我得到的值。 I have googled this but found a surprising lack of detailed info about it. 我用谷歌搜索了这个,但发现了一个令人惊讶的缺乏关于它的详细信息。 The best I could find was the following from cplusplus.com 我能找到的最好的是来自cplusplus.com的以下内容

Converting to int from some smaller integer type, or to double from float is known as promotion, and is guaranteed to produce the exact same value in the destination type. 从某个较小的整数类型转换为int,或从float转换为double,称为提升,并保证在目标类型中生成完全相同的值。 Other conversions between arithmetic types may not always be able to represent the same value exactly: 算术类型之间的其他转换可能并不总是能够完全表示相同的值:

 If the conversion is from a floating-point type to an integer type, the value is truncated (the decimal part is removed). The conversions from/to bool consider false equivalent to zero (for numeric types) and to null pointer (for pointer types); and true equivalent to all other values. Otherwise, when the destination type cannot represent the value, the conversion is valid between numerical types, but the value is implementation-specific (and may not be portable). 

This suggestion that the results are implementation defined does not surprise me, but I have heard that cplusplus.com is not always reliable. 这个结果是实现定义的建议并不让我感到惊讶,但我听说cplusplus.com并不总是可靠的。

Finally, when performing the same cast from a 32 bit integer to 16 bit integer (again with a value outisde of 16 bit range) I saw results clearly indicating integer overflow. 最后,当从32位整数到16位整数执行相同的转换(同样具有16位范围的值)时,我看到结果清楚地表明整数溢出。 Although I was not surprised by this it has added to my confusion due to the inconsistency with the cast from float type. 虽然我对此并不感到惊讶,但由于与float类型的演员不一致而增加了我的困惑。

I have no access to the C++ standard, but a lot of C++ people here do so I was wondering what the standard says on this issue? 我无法访问C ++标准,但是这里有很多C ++人员,所以我想知道标准在这个问题上说了些什么? Just for completeness, I am using g++ version 4.6.3. 为了完整起见,我使用的是g ++版本4.6.3。

You're right to question what you've read. 你对自己所读到的内容有疑问。 The conversion has no defined behaviour, which contradicts what you quoted in your question. 转换没有明确的行为,这与您在问题中引用的内容相矛盾。

4.9 Floating-integral conversions [conv.fpint] 4.9浮动积分转换[conv.fpint]

1 A prvalue of a floating point type can be converted to a prvalue of an integer type. 1浮点类型的prvalue可以转换为整数类型的prvalue。 The conversion truncates; 转换截断; that is, the fractional part is discarded. 也就是说,丢弃小数部分。 The behavior is undefined if the truncated value cannot be represented in the destination type. 如果截断的值无法在目标类型中表示,则行为未定义。 [ Note: If the destination type is bool , see 4.12. [ 注意:如果目的地类型是bool ,请参阅4.12。 -- end note ] - 结束说明 ]

One potentially useful permitted result that you might get is a crash. 您可能获得的一个可能有用的允许结果是崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 标准对堆栈溢出有何规定? - What does the C++ standard say about stack overflow? C++20 标准对使用子对象作为模板非类型 arguments 有什么看法? - What does the C++20 standard say about usage of subojects as template non-type arguments? 关于使用noexcept覆盖throw()函数,C ++标准有何说法? - What does the C++ standard say about overriding a throw() function with noexcept? C ++语言标准对static_cast如何处理减小整数大小有何看法? - What does the C++ language standard say about how static_cast handles reducing the size of an integer? 关于移动活动对象存储,C ++标准怎么说? - What does C++ standard say about moving live object storage? 在C ++标准中它表示必须初始化const内置类型变量的定义? - Where in the C++ Standard does it say that the definition of a const built-in type variable must be initialized? C ++ 14标准对auto作为参数类型的说法是什么 - What does the C++14 standard say regarding auto as argument type 依赖的非类型参数包:标准说什么? - Dependent non-type parameter packs: what does the standard say? C++ 标准 state int, long 类型的大小是多少? - What does the C++ standard state the size of int, long type to be? 标准对这个指向成员函数类型的模板参数有什么看法? 我的代码是错误的,还是 MSVS 16.6 有问题? - What does the standard say about this pointer-to-member-function type template parameter? Is my code wrong, or is MSVS 16.6 buggy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM