警告C26454:算术溢出:“-"运算在编译时产生负无符号结果(io.5) [英] Warning C26454: Arithmetic overflow: '-' operation produces a negative unsigned result at compile time (io.5)
问题描述
代码分析:
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_HISTORY_TYPE,
&CAssignHistoryDlg::OnTcnSelchangeTabHistoryType)
警告C26454:
算术溢出:-"运算产生负无符号结果 在编译时(io.5).
Arithmetic overflow: '-' operation produces a negative unsigned result at compile time (io.5).
TCN_SELCHANGE
的定义是:
#define TCN_FIRST (0U-550U)
#define TCN_SELCHANGE (TCN_FIRST - 1)
我看不到我还能做些什么!
I can't see what else I can do!
推荐答案
您正在尝试从较小的无符号值中减去较大的无符号值,这会导致结果换行为零.在您的情况下,我假设将TCN_FIRST定义为0,因此将TCN_SELCHANGE设置为1将解决此问题.
You are trying to subtract a larger unsigned value from a smaller unsigned value and it's causing the result to wrap past zero. In your case, I assume TCN_FIRST is defined as 0 so setting TCN_SELCHANGE to one will fix the problem.
您还应该使用constexpr
或const
而不是定义.
You should also be using constexpr
or const
instead of defines anyway.
根据MSDN:
C26451 RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE :[操作员]操作结束了0,并在编译时产生一个大的无符号数.此警告表明减法运算会产生否定结果,该结果在无符号上下文中进行了评估.这将导致结果回绕到0并产生一个非常大的无符号数字,这可能导致意外的溢出.
C26451 RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE :[operator] operation wraps past 0 and produces a large unsigned number at compile time. This warning indicates that the subtraction operation produces a negative result which was evaluated in an unsigned context. This causes the result to wrap past 0 and produce a really large unsigned number, which can result in unintended overflows.
1 // Example source:
2 unsigned int negativeunsigned() {
3 const unsigned int x = 1u - 2u; // C26454 reported here
4 return x;
5 }
1 // Corrected source:
2 unsigned int negativeunsigned() {
3 const unsigned int x = 4294967295; // OK
4 return x;
5 }
在更正后的源中,为未签名的结果分配了一个正值.
In the corrected source, a positive value was assigned to the unsigned result.
这篇关于警告C26454:算术溢出:“-"运算在编译时产生负无符号结果(io.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!