I don't know why (-1 < s.size()) evaluates to false, when -1 is less than 3.
Code:int main()
{
string s = "abc";
cout << s.size() << '\n';
if (-1 < s.size()) {
cout << "yes";
} else {
cout << "no";
}
keep_window_open();
}
Printable View
I don't know why (-1 < s.size()) evaluates to false, when -1 is less than 3.
Code:int main()
{
string s = "abc";
cout << s.size() << '\n';
if (-1 < s.size()) {
cout << "yes";
} else {
cout << "no";
}
keep_window_open();
}
s.size() returns a string::size_type, which is indirectly a typedef for size_t, which is an unsigned integer type (typically itself a typedef for unsigned int).
So, -1 < s.size() is a comparison between a signed and unsigned integer. The conversion of -1 to an unsigned integer resulted in a number larger than what s.size() returned.
Is there an easy way to fix this?
Yes, just write:Quote:
Is there an easy way to fix this?
Code:int main()
{
string s = "abc";
cout << s.size() << '\n';
cout << "yes";
keep_window_open();
}
laserlight: haha lolo! :D
really strange, one unsigned other signed and they promote the signed to unsigned?
actually i think you would get a warning, which will ask for a cast, and this will fix the problem, before it annoys you.
Yes, it should indeed prompt a warning. When comparing integers, make sure both are of the same type.
I'm using Visual C++ and it didn't warn me. I didn't change any warning levels either.
All Visual Studio versions I've used has warned on signed/unsigned comparison, even on default warning levels. What version are you using?
Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM
In project settings, under C/C++, make sure "Warning level" is set to 4.
In the output window, check for warnings in the output.
If using the Task List, make sure the Warning button isn't deselected (thus silencing the warnings).
You should get warnings about it.
But W4 is the only one that can warn about potentially uninitialized variables and unused variables and the use of non-standard extensions, so I suggest leaving it on W4 (level 4).
This is weird, I tested this on MSVC8 at /W4 and it compiled without any warnings or errors:
The MinGW port of g++ 3.4.5 reports a warning with -W.Code:#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers(10);
std::cout << (-1 < numbers.size() ? "yes" : "no") << std::endl;
}
Heh, seems we've found a dent in the armor. The compiler seems to ignore signed and unsigned comparison when one number is a "magic number", but if they're variables, it warns:
Code:std::vector<int> numbers(10);
int32_t t1 = -1;
uint32_t t2 = numbers.size();
std::cout << (t1 < t2 ? "yes" : "no") << std::endl; // Warning
std::cout << ((signed)-1 < (unsigned)2); // No warning
Code:#include <iostream>
#include <vector>
int main ()
{
std::vector<int> numbers(10);
int32_t t1 = -1;
uint32_t t2 = numbers.size();
std::cout << (t1 < t2 ? "yes" : "no") << std::endl; // Warning
std::cout << ((signed)-1 < (unsigned)2); // No warning
return 0;
}
/* my output
no
0
*/
Quote:
main.cpp
****** {BD Software Proxy c++ v3.43a for gcc} STL Message Decryption is Off ******
main.cpp: In function `int main()':
main.cpp:9: warning: comparison between signed and unsigned integer expressions
main.cpp:10: warning: comparison between signed and unsigned integer expressions