Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 假设if语句中不发生签名溢出_C++_Qt_G++ - Fatal编程技术网

C++ 假设if语句中不发生签名溢出

C++ 假设if语句中不发生签名溢出,c++,qt,g++,C++,Qt,G++,为什么会出现这个警告?如果我检查边界,这实际上不是一个假设。如何修复 如果num\u actions\u to\u skip设置为1而不是2,则错误消失 谢谢 error: assuming signed overflow does not occur when assuming that (X - c) <= X is always true [-Werror=strict-overflow] cc1plus: all warnings being treated as errors

为什么会出现这个警告?如果我检查边界,这实际上不是一个假设。如何修复

如果
num\u actions\u to\u skip
设置为1而不是2,则错误消失

谢谢

error: assuming signed overflow does not occur when assuming that (X - c) <= X is always true [-Werror=strict-overflow]
cc1plus: all warnings being treated as errors
由此:

-Wstrict溢出

-Wstrict溢出=n

仅当-fstrict overflow处于活动状态时,此选项才处于活动状态。它警告编译器基于不发生签名溢出的假设进行优化的情况。注意,它并没有警告代码可能溢出的所有情况:它只警告编译器实现某些优化的情况。因此,此警告取决于优化级别

假设不发生有符号溢出的优化是完全安全的,前提是所涉及的变量的值实际上永远不会发生溢出。因此,这个警告很容易给出错误的肯定:关于实际上不是问题的代码的警告。为了帮助关注重要问题,定义了几个警告级别。在估计循环需要多少次迭代时,特别是在确定是否执行循环时,不会对使用未定义的有符号溢出发出警告


-Wstrict溢出=1

警告可疑且容易避免的案例。例如,使用-fstrict overflow,编译器将x+1>x简化为1。此级别的-Wstrict溢出由-Wall启用;更高的级别不是,而且必须明确请求

-Wstrict溢出=2

还警告其他情况,其中比较被简化为常数。例如:abs(x)>=0。这只能在-fstrict溢出生效时进行简化,因为abs(INT_MIN)溢出到INT_MIN,该值小于零-Wstrict overflow(无级别)与-Wstrict overflow=2相同

-Wstrict溢出=3

还警告其他简化比较的情况。例如:x+1>1简化为x>0

-Wstrict溢出=4

同时警告上述案例中未涉及的其他简化。例如:(x*10)/5被简化为x*2

-Wstrict溢出=5

还警告编译器减少比较中涉及的常量大小的情况。例如:x+2>y简化为x+1>=y。这仅在最高警告级别报告,因为这种简化适用于许多比较,因此此警告级别会产生大量误报

由此:

-Wstrict溢出

-Wstrict溢出=n

仅当-fstrict overflow处于活动状态时,此选项才处于活动状态。它警告编译器基于不发生签名溢出的假设进行优化的情况。注意,它并没有警告代码可能溢出的所有情况:它只警告编译器实现某些优化的情况。因此,此警告取决于优化级别

假设不发生有符号溢出的优化是完全安全的,前提是所涉及的变量的值实际上永远不会发生溢出。因此,这个警告很容易给出错误的肯定:关于实际上不是问题的代码的警告。为了帮助关注重要问题,定义了几个警告级别。在估计循环需要多少次迭代时,特别是在确定是否执行循环时,不会对使用未定义的有符号溢出发出警告


-Wstrict溢出=1

警告可疑且容易避免的案例。例如,使用-fstrict overflow,编译器将x+1>x简化为1。此级别的-Wstrict溢出由-Wall启用;更高的级别不是,而且必须明确请求

-Wstrict溢出=2

还警告其他情况,其中比较被简化为常数。例如:abs(x)>=0。这只能在-fstrict溢出生效时进行简化,因为abs(INT_MIN)溢出到INT_MIN,该值小于零-Wstrict overflow(无级别)与-Wstrict overflow=2相同

-Wstrict溢出=3

还警告其他简化比较的情况。例如:x+1>1简化为x>0

-Wstrict溢出=4

同时警告上述案例中未涉及的其他简化。例如:(x*10)/5被简化为x*2

-Wstrict溢出=5

还警告编译器减少比较中涉及的常量大小的情况。例如:x+2>y简化为x+1>=y。这仅在最高警告级别报告,因为这种简化适用于许多比较,因此此警告级别会产生大量误报


你只需要重新思考你的逻辑

static const int num_actions_to_skip = 2;
const int loc = action_list.count() - num_actions_to_skip;
if (loc >= 0 && loc < action_list.count()) {
    // ...
}
这相当于(如编译器警告所述,假设没有发生溢出):

重新替换原始条款,这给了我们:

static const int num_actions_to_skip = 2;
// const int loc = action_list.count() - num_actions_to_skip;
if (action_list.count() >= num_actions_to_skip) {
    // ...
}
编译器警告您,如果存在整数溢出,它正在执行的优化可能无效(允许假定没有溢出,因为如果存在,则行为未定义)。它很好地警告了您这一点——这对您来说是幸运的,因为它指出了这样一个事实,即您的代码正在做一些它不需要做的事情

我不知道您是否需要保留
loc
的声明;这取决于你以后是否使用它。但是如果您按照我建议的方式简化代码,它应该以同样的方式工作,并且更易于阅读和理解

如果您从编译器收到警告消息,您的目标不应该只是让消息消失;这应该是深入研究,找出编译器警告您的内容
tool_menu->insertAction(action_list.at(action_list.count() - 2),
                                action);
static const int num_actions_to_skip = 2;
const int loc = action_list.count() - num_actions_to_skip;
if (loc >= 0 && loc < action_list.count()) {
    // ...
}
if (count - 2 >= 0 && count - 2 < count)
if (count >= 2 && -2 < 0)
if (count >= 2)
static const int num_actions_to_skip = 2;
// const int loc = action_list.count() - num_actions_to_skip;
if (action_list.count() >= num_actions_to_skip) {
    // ...
}
const QList<QAction *> &action_list = tool_menu->actions();
static const unsigned int num_actions_to_skip = 2;
const unsigned int pos = action_list.count() - num_actions_to_skip;
assert(pos >= 0);
tool_menu->insertAction(action_list.at(pos),
                        action);
static inline bool shape_int_rectangle_contains ( 
    const shape_int_rectangle_t *this_, int32_t x, int32_t y )
{
    bool result;
    const int32_t right = (*this_).left + (*this_).width;
    const int32_t bottom = (*this_).top + (*this_).height;
    /*
     * warning: assuming signed overflow does not occur when assuming that (X + c) >= X is always true [-Wstrict-overflow]
     *
     * result = ( x >= (*this_).left )&&( y >= (*this_).top )&&( x < right )&&( y < bottom );
     *
     * fix:
     */
    result = ( x-(*this_).left >= 0 )&&( y-(*this_).top >= 0 )&&( x-right < 0 )&&( y-bottom < 0 );
    return result;
}