我可以选择在 C/C++ 中限制还是溢出整数值吗?或者那些编译依赖?
最佳答案
您所说的“上限”被称为“饱和”。数字信号处理器 (DSP) 支持硬件饱和是很常见的,但大多数微处理器自然会溢出和环绕。支持 DSP 扩展(例如 MMX)的处理器也支持饱和。
对饱和的语言支持通常是通过编译器扩展、内在函数和库实现的。在 C++ 中也许可以创建一个用于饱和算术类型的模板类。
来 self 对this question的回答:
ISO/IEC JTC1 SC22 WG14 N1169 (Programming languages - C - Extensions to support embedded processors) specifies a
_Sat
type qualifier for saturating data types. I have never tried to use it in any compiler, but it is included in the GCC 4.x documentation.VC++ 2003 onward supports MMX intrinsics that allow saturating arithmetic.
请注意,在没有硬件支持饱和算术的处理器上,使用它可能会影响性能。
关于c++ - 我可以指定在 C/C++ 中是否限制或溢出整数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16758082/