Bug 1108339 - Bogus "narrowing conversion of" failure for char bitwise or
Summary: Bogus "narrowing conversion of" failure for char bitwise or
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc
Version: 20
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2014-06-11 18:37 UTC by don
Modified: 2014-06-30 14:00 UTC (History)
4 users (show)

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2014-06-30 14:00:45 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)
foo.cpp (95 bytes, text/plain)
2014-06-11 18:37 UTC, don
no flags Details

Description don 2014-06-11 18:37:54 UTC
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.

Comment 1 don 2014-06-11 18:41:00 UTC
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: & + / *

Comment 2 don 2014-06-11 18:56:24 UTC
This same issue where non-const char fails and const char succeeds also applies to extended initializer lists (using -std=c++11).

Comment 3 Jakub Jelinek 2014-06-24 15:41:44 UTC
Why do you think this is a bug?  In C/C++, for {,signed,unsigned} {char,short} x
the expression x | x has type int.

Comment 4 don 2014-06-25 20:22:20 UTC
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?

Comment 5 Jason Merrill 2014-06-30 14:00:45 UTC
(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.


Note You need to log in before you can comment on or make changes to this bug.