I've seen this kind of question asked before, but supplied answers don't clear everything for me. When this question is posted it is usually accompanied by next example:
#include <iostream>
int main()
{
unsigned int u = 10;
int i = -42;
std::cout << i + i << std::endl;
std::cout << i + u << std::endl;
return 0;
}
Output:
-84
4294967264
All works as expected int is converted to unsigned. But if absolute value of i is less than u it seems like no such conversion is hapenning.
#include <iostream>
int main()
{
unsigned int u = 10;
int i = -3;
std::cout << i + i << std::endl;
std::cout << i + u << std::endl;
return 0;
}
Output:
-6
7
I haven't seen any answer mentioning it and can't find any explanation. Even though it seems like a logical thing to happen I havent thing any explanation accounting for this.