void foo(bool bar)
{
}
int main(void)
{
foo("Whose bright idea was this?");
}
No error, not even a warning, perfectly legit code. If bar had been declared int or char, you'd get an error. Instead, your code ends up less safe because you used bool, thinking you were doing the right thing. Yes, they wanted to allow it anywhere a conditional expression would fit, but implicit conversion from anything other than int and char would have been a good place to draw the line.So I wouldn't be surprised if the GP example was prompted by a similar design flaw in either the language or a particular compiler.
> The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.
https://port70.net/~nsz/c/c11/n1570.html#6.5.9p3
Thanks for clearing that up. (Although I feel a bit foolish now.)
That ternary operator "?" is just an if-statement which should have the same "problem" with an int argument. It is a broken compiler that would complain about an if statement but an identical ?-statement.
If the int nature of equals-to was a problem, you'd have to use limit its use to switch statements.
The strict requirement in the standard is just a 'scalar type': every conditional expression in C, even `if (true)`, technically evaluates on unequal comparison to zero. See section 6.8.4.1 "The if statement" of the standard:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf
This applies to ternary operators, if statements, do/while loops, for loop termination conditions, etc.
However, when defining a function that returns boolean, you've asked the compiler to make your program more strict than just returning an integer that's zero or nonzero, you're not allowed to return anything but 'true' or 'false' - no, you're not allowed to pack in error codes after the fact!
There is nothing to understand. Sorry for being a bit dense.
Then there's living in C++ land, which is 1000 times worse.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/lin...
Oh man, c'mon. That kind of thing bugs me. Just "return reg == LTC_REG_CONFIG" already, sheesh. (And maybe make it a preprocessor directive while you're at it?)