I have the following code:
#include <iostream> using namespace std; int main(int argc, char *argv[]) { string a = "a"; for(unsigned int i=a.length()-1; i+1 >= 1; --i) { if(i >= a.length()) { cerr << (signed int)i << "?" << endl; return 0; } } }
If I compiled in MSVC with full optimization, the output I get is "-1?". If I compile in debug mode (without optimization), I do not get output (expected.)
I thought the standard ensures that unsigned integers are overflowed in a predictable way, so when I = (unsigned int) (- 1), I + 1 = 0, and the loop condition I + 1> = 1 fails. Instead, the test somehow passes. Is this a compiler error, or am I doing something undefined somewhere?
c ++ standards
Etienne vouga
source share