[Solved] Function overloaded by bool and enum type is not differentiated while called using multiple ternary operator in C++


That is because the ternary operator always evaluates to a single type. You can’t “return” different types with this operator.

When the compiler encounters such an expression he tries to figure out whether he can reduce the whole thing to one type. If that’s not possible you get a compile error.

In your case there is a valid option using bool as a type. Because cfg->dic is an enum type which is implicitly convertible to bool. If you would use and enum class your code would not compile anymore showing you what your actual problem is (example).

Also I don’t really see what the advantage of this kind of code is. In my opinion it makes the code much harder to read. You could reduce your ifs to just one, if you’re concerned about too many of them:

if(acc == VirtualGpio::Pin)
  setCfgSetting(&cfg->pin,cfg->dic);
else
  setCfgSetting(&cfg->pin, cfg->dic == VirtualGpio::INPUT);

1

solved Function overloaded by bool and enum type is not differentiated while called using multiple ternary operator in C++