"Jim Langston" <ta*******@rocketmail.comwrote in message
news:CE***************@newsfe03.lga...
Expected output of this program is:
1
-1
-1
-1
Using Microsoft Visual C++ .net 2003 actual output is:
1
4294967295
4294967295
4294967295
Can someone explain why to me?
#include <string>
#include <iostream>
#include <map>
std::map<int, std::stringFoo;
int main()
{
Foo[1] = "Hello";
std::cout << Foo.size() << "\n";
std::cout << -Foo.size() << "\n";
std::cout << 0 - Foo.size() << "\n";
std::cout << 0 - (Foo.size()) << std::endl;
std::string wait;
std::getline( std::cin, wait );
} // function main
Hmm.. I was able to get the expected output this way:
#include <string>
#include <iostream>
#include <map>
std::map<int, std::stringFoo;
int main()
{
Foo[1] = "Hello";
std::cout << Foo.size() << "\n";
std::cout << -static_cast<int>( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int>( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int>( (Foo.size()) ) << std::endl;
std::string wait;
std::getline( std::cin, wait );
} // function main
I can understand why I have to do this, but I don't understand why I should
have to do this. It is unexpected.
The actual application was like this:
float x = (- Models.size() / 2 + ModelCount ) * 10;
I understand that size() returns an unsigned type, and since it is one, is
similar to sayign something like: static_cast<unsigned int>( -1 )
Man, thsi is really a gotcha.