How is the return type of a ternary operator determined?

It's easy to see the mistake when the parsing order is visualized:

std::cout << (abs(c2-c1) == abs(r2-r1)) ? 1 : 2 << std::endl;
\_______/                                                      <--- #1
             \________________________/   V   \~~~~error~~~/   <--- #2
             \_____________________________________________/   <--- #3
\__________________________________________________________/   <--- #4
\___________________________________________________________/  <--- #5

This has nothing to do with how the return type is deduced and everything to do with operator precedence. When you have

std::cout << (abs(c2-c1) == abs(r2-r1)) ? 1 : 2 << std::endl;

it isn't

std::cout << ((abs(c2-c1) == abs(r2-r1)) ? 1 : 2) << std::endl;

because ?: has lower precedence than <<. That means what you actually have is

(std::cout << (abs(c2-c1) == abs(r2-r1))) ? 1 : (2 << std::endl);

and this is why you get an error about an <unresolved overloaded function type>. Just use parentheses like

std::cout << ((abs(c2-c1) == abs(r2-r1)) ? 1 : 2) << std::endl;

and you'll be okay.


Due to operator precedence, that line is treated as:

(std::cout << (abs(c2-c1) == abs(r2-r1))) ? 1 : (2 << std::endl);

Change it to

std::cout << ((abs(c2-c1) == abs(r2-r1)) ? 1 : 2) << std::endl;
//           ^----------------------------------^
//           Surrounding parentheses

You have to put parentheses around a ternary operation:

std::cout << ((abs(c2-c1) == abs(r2-r1)) ? 1 : 2) << std::endl;

If not the the << operator goes to the 2 and it gives an error because it doesn't have such overloaded function.

This happens because the bitwise left shift operator (<<) has a higher precedence than the ternary operator. You can see the full list of operators and their precedence in this page of the C++ reference.