简体   繁体   中英

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:

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() . 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 ). The suggestion given seems off-topic to me. Has the syntax for two iterations in one for-loop changed in C++11?

(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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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