Why does the 2nd and 3rd assignments in f2
produces this and the first doesn't?
Clang-Tidy: Narrowing conversion from 'int' to signed type 'char' is implementation-defined
AFAIK, according to C language specification the type of the ternary operator result is determined by the type of the first return clause, ergo, the type of X ? Y : Z
is the type of Y
, so the type of test() ? f1() : 'b'
and test() ? 'b' : f1()
should be char.
However, the third line doesn't produce this warning, so apparently clang-tidy thinks that the type of test() ? f1() : 'b'
is int
...
int a;
int test() { return a; }
char f1() { return 'x'; }
void f2() {
char x = f1(); // fine
char y = test() ? f1() : 'b'; // clang-tidy "narrowing conversion"
char z = test() ? 'b' : f1(); // clang-tidy "narrowing conversion"
char w = (char) (test() ? f1() : 'b'); // fine
}
P.S. please don't refer me to this post. There is no relation.