This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: warning: conversion from ‘int’ to ‘char’ may change value
在 2018/9/20 22:08, Vincent Lefevre 写道:
>> In C++, declaring n1 const avoids the warning regardless of
>> optimization levels.
>
> If the constant propagation is done at -O0, this could explain
> the behavior.
>
> Or do you mean that GCC remembers the type the data come from,
> i.e. assuming char is signed, if n1 is of type char, then ~n1
> is necessarily representable in a char, thus can be regarded
> as of being of type char in its analysis?
>
In C++ adding the `const` qualifier makes `~n1` a constant expression.
In C it never is, regardless of qualifiers.
BTW, I am quite disappointed with such 'false' warnings, because by
performing a compound AND-and-ASSIGN operation on a `char` object I have
no interest in bits that don't fit into a `char`, be they ones or
zeroes. Perhaps there are scenarios where they shouldn't be ignored, but
I can't think of any.
--
Best regards,
LH_Mouse