[Bug c++/93589] Template instantiation creates a conversion warning when it should not

jrdowning at yahoo dot com gcc-bugzilla@gcc.gnu.org
Fri Feb 21 02:11:00 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93589

--- Comment #8 from John Downing <jrdowning at yahoo dot com> ---

> (In reply to John Downing from comment #5)
> > This will generate a warning, for the char and short cases, when the "|="
> > happens.  This is despite "val" being explicitly declared as a char or an
> > short.
> 
> It's not *despite* being declared as char or short, it's *because* they are
> declare as char and short.

This means that the "|=" operator or the "<<" operator changes the RHS side to
an int?  While that is within the guidelines of the cpp standard, the standard
as referenced in this thread says "can" change type, not "must".  So then why
does this change happen?

>  
> > I can see this being correct if "val" has been declared as "unsigned", which
> > i shorthand for "unsigned int".  But if "val" is explicitly declared as
> > something, there should be a potential for the conversion to change the
> > value.
> 
> That's not true. Given:

You are right, that's my opps - I meant to say "there should *no* potential".

> 
>   val |= (CHAR_BIT * static_cast<char>(i)) << (CHAR_BIT *
> static_cast<char>(i));
> 
> The type of (CHAR_BIT * static_cast<char>(i)) is int, due to:
> https://en.cppreference.com/w/cpp/language/
> implicit_conversion#Integral_promotion

It is this the reference which uses "can" not "must".

> 
> And the type of the entire right-hand side is int, so you are doing val |=
> int(some_value) which the compiler correctly says might not fit in a short
> or char. In this specific case, your loop conditions mean that the value of
> the right hand side will fit, but the warning doesn't know about the values,
> it only cares that conversion from int to short int involves a potentially
> lossy conversion.

Yes, and with the standard using the word "can" to change the RHS to an int, it
is a "design decision".


More information about the Gcc-bugs mailing list