+ 3
A question
#include <iostream> using namespace std; int f(int a){ return ++a; } int f(unsigned int a){ return --a; } int main() { cout<<f(5); return 0; } How the compiler make a decision between these two function? Output is 6.
3 Answers
+ 2
Numbers are handled as integers if not casted into something else. When casting the argument to unsigned into it runs the other function.
+ 4
cout<<f((unsigned)5)
displays 4
5 in your example defaults to type int, and thus displays 6.
+ 2
f(5U) is 4 because the U tells the compiler that it's unsigned.