c++ - kill unsigned / signed comparison error -
in general, want warnings of unsigned vs signed.
however, in particular case, want suppressed;
std::vector<blah> blahs; for(int = 0; < blahs.size(); ++i) { ... i want kill comparison.
thanks!
(using g++)
you should fix, not suppress. use unsigned type:
for (size_t = 0; < blahs.size(); ++i) you can use unsigned, size_t more appropriate here (and may have different, larger, range). if you're using i iterate , don't need value in loop, use iterators instead:
for (auto iter = blahs.begin(), end = blahs.end(); iter != end; ++iter) if compiler not support auto, replace auto t::iterator or t::const_iterator, t type of blahs. if compiler supports fuller subset of c++11, though, this:
for (auto& element : blahs) which best of all.
strictly speaking, above not "correct". should be:
typedef std::vector<blah> blah_vec; blah_vec blahs; (blah_vec::size_type = 0; < blahs.size(); ++i) but can verbose, , every implementation know uses size_t size_type anyway.
if reason need signed integer type i, you'll have cast:
// assumes size() fit in int (int = 0; < static_cast<int>(blahs.size()); ++i) // assumes not negative (so use unsigned type!) (int = 0; static_cast<size_t>(i) < blahs.size(); ++i) // , technically correct way, assuming not negative (int = 0; static_cast<blah_vec::size_type>(i) < blahs.size(); ++i)
Comments
Post a Comment