[Bug c++/41274] New: Some -Wconversion warnings are almost impossible to fix

ashaduri at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Sep 5 12:19:00 GMT 2009


Some -Wconversion warnings are just useless and almost impossible to fix.
Consider the following example:

--------------------------------------------------------------------

template <typename T> inline
void add_to_v1(T& v1, const T& v2)
{
        v1 += v2;  // warning: conversion to ‘short int’ from ‘int’ may alter
its value
}

int main()
{
        {
                short i1 = 0;
                short i2;
                i1 += i2;  // warning: conversion to ‘short int’ from ‘int’ may
alter its value
        }
        {
                short i1 = 0;
                short i2;
                i1 /= i2;  // warning: conversion to ‘short int’ from ‘int’ may
alter its value
        }
        {
                char c1 = 0;
                char c2;
                c1 += c2;  // warning: conversion to ‘char’ from ‘int’ may
alter its value
        }
        {
                short v1;
                add_to_v1(v1, (short)2);  // see function template above
        }
        return 0;
}

--------------------------------------------------------------------

As you can see, a simple += operator between short integers causes the warning,
which is, of course, a standard C/C++ feature with defined behaviour. Same goes
for the other ones, like /= operator.

The worse thing about it is that it's impossible to shut it up when using
function templates, without making specializations for every type for which
sizeof(T) < sizeof(int). The += operator in the function template above is
expected to work with any T supporting operator+=. Instead, we get an
essentially non-fixable warning there. Since a+=b and a=a+b mean different
things depending on type, such warnings cannot be fixed with static_cast.

gcc version 4.4.1 [gcc-4_4-branch revision 150839] (SUSE Linux)


-- 
           Summary: Some -Wconversion warnings are almost impossible to fix
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ashaduri at gmail dot com
 GCC build triplet: x86_64-suse-linux
  GCC host triplet: x86_64-suse-linux
GCC target triplet: x86_64-suse-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41274



More information about the Gcc-bugs mailing list