我猜C只是一个重新解释,并且位模式保持原样.
我试图理解当我将更宽的有符号值转换为更窄的有符号值时会发生什么.是保留符号,还是简单地将较窄宽度中的值解释为带符号值.
谢谢你的帮助.
编辑 – 这是针对C99编译的c代码.问题不在于c编译器将使用粗糙的旧样式转换,而是如果有什么定义C的作用.
如果你只关心C,你真的不需要询问C,但在你的例子中,C和C仍然做大部分相同的事情;
C说这些完整的转换:
If the destination type is signed,the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise,the value is implementation-defined.
C99说这些积分转换:
Otherwise,the new type is signed and the value cannot be represented in it; either the
result is implementation-defined or an implementation-defined signal is raised.
如果可能的话,将较大的整数值转换为较小的有符号整数值将产生相同的值,否则给出实现定义(即,非可移植)行为.
§5.4/ 4州
The conversions performed by
- a const_cast (5.2.11),
- a static_cast (5.2.9),
- a static_cast followed by a const_cast,
- a reinterpret_cast (5.2.10),or
- a reinterpret_cast followed by a const_cast,
can be performed using the cast notation of explicit type conversion.
[…]
If a conversion can be interpreted in more than one of the ways listed above,the interpretation that
appears first in the list is used,even if a cast resulting from that interpretation is ill-formed.
定义C使其与C兼容;也就是说,C强制转换将在C中执行与在C中相同的操作.这意味着C中的强制转换符号实际上根据之间的类型执行不同的操作.