Created attachment 907795 [details] foo.cpp Description of problem: gcc fails with: foo.cpp: In function 'int main()': foo.cpp:3:26: error: narrowing conversion of '(int)x' from 'int' to 'const char' inside { } is ill-formed in C++11 [-Werror=narrowing] const char y[] = { x | x }; Version-Release number of selected component (if applicable): gcc version 4.8.2 20131212 (Red Hat 4.8.2-7) (GCC) How reproducible: Always Steps to Reproduce: g++ -Wall -Werror foo.cpp Actual results: Failure. Expected results: Success. Additional info: This only fails with both variables of type "char" or "unsigned char". With both variables using other types, such as "long" and "int" it works fine. It also works if the first variable is declared const.
Also note that the error message makes no sense. It says "error: narrowing conversion of '(int)x'" as if it thinks the "x" variable is being cast to "int" prior to applying the bitwise operation. These other operators create similar failures: & + / *
This same issue where non-const char fails and const char succeeds also applies to extended initializer lists (using -std=c++11).
Why do you think this is a bug? In C/C++, for {,signed,unsigned} {char,short} x the expression x | x has type int.
Is it really possible that the spec says "x|x" is of type int for x of type char? And yet "x|x" is of type char for x of type const char? I wasn't able to find any evidence that the spec is so bizarre as to require that. At a minimum though the error message is wrong, since there is no '(int)x' anywhere. Perhaps it means to say '(int)(x | x)', if the spec requires an implicit type conversion there?
(In reply to don from comment #4) > Is it really possible that the spec says "x|x" is of type int for x of type > char? Yes. For most binary operators, the operands are subject to the "usual arithmetic conversions", which includes integral promotion. I agree that it is unfortunate that this results in a narrowing error in this case, but G++ is following the C++11 standard. I encourage you to use -Wno-narrowing to avoid the diagnostic.