在我的例子中,如何对微软C26451 (算术溢出)警告进行防御性编码?我觉得这应该是微不足道的。太让人沮丧了!
// Linear interpolation
// target - the target point, 0.0 - 1.0
// x... - two values {X1} {X2}
inline static double Linear(float target, double x1, double x2) {
return (target * x2) + ((1.0 - (double)target) * x1);
}
我通读了Arithmetic overflow: Using operator '*' on a 4 byte value then casting the result to a 8 byte value,但似乎无法修复我的C26451警告:“算术溢出:在4字节值上使用运算符'-‘,然后将结果转换为8字节值。在调用运算符'-’之前将值转换为更宽的类型,以避免溢出(io.2)。
我该怎么做才能删除警告?
Microsoft文档对他们编译错误并没有真正的帮助。https://docs.microsoft.com/en-us/cpp/code-quality/c26451
发布于 2021-04-27 14:52:30
编译器警告没有意义,较新的Visual Studio版本不会给出同样的警告。我只对这一行禁用它:
inline static double Linear(double target, double x1, double x2) {
#pragma warning( push )
#pragma warning( disable : 26451 )
return (target * x2) + ((1.0 - target) * x1);
#pragma warning( pop )
}
https://stackoverflow.com/questions/67277693
复制相似问题