简体   繁体   English

不等式比较结果移至C ++ 11时未使用的警告

[英]inequality comparison result unused warning when moving to C++11

I just moved to C++11 and noticed some new warnings on my old code: 我刚刚移到C ++ 11,并在旧代码中注意到了一些新警告:

ChinaminiC.cpp:70:76: warning: inequality comparison result unused [-Wunused-comparison]
for(std::vector<std::string>::const_iterator cit = _names.begin(); 
    cit != _names.end(), i < _names.size(); 
    cit++, i++)
ChinaminiC.cpp:70:76: note: use '|=' to turn this inequality comparison into an or-assignment

The inequality referred to is the one from cit != _names.end() . 引用的不等式是cit != _names.end()的不等式。 If this means that the inequality condition is not checked then that is a problem (the double iteration with i is there to iterate through an argument that is same size as _names ). 如果这意味着不检查不等式条件,那就是一个问题(使用i的双重迭代可以通过与_names大小相同的参数进行迭代)。 The suggestion given seems off-topic to me. 给出的建议对我来说似乎是题外话。 Has the syntax for two iterations in one for-loop changed in C++11? 在C ++ 11中,一个for循环中两次迭代的语法是否已更改?

(cit != _names.end()) && (i < _names.size());

The , operator evaluates the left side, and discards the result. ,运算符评估左侧,并舍弃结果。 That's not what you want. 那不是你想要的。 You need to combine those two tests with && (or || ). 您需要将这两个测试与&& (或|| )结合使用。

No, the syntax hasn't changed. 不,语法没有改变。

cit != _names.end(), i < _names.size()

Here you are using the comma operator, which means that the left side will be evaluated then discarded, and then the right side will be evaluated. 在这里,您使用的是逗号运算符,这意味着将先评估左侧然后丢弃,然后再评估右侧。 The result of the expression is the result of the right side of the comma. 表达式的结果是逗号右边的结果。

If you want to make sure that both conditions are true, you need to use logical AND: 如果要确保两个条件都成立,则需要使用逻辑AND:

cit != _names.end() && i < _names.size()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM