public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/51294] New: spurious warning from -Wconversion in C and C++
@ 2011-11-24 11:43 tortoise_74 at yahoo dot co.uk
  2011-11-24 11:51 ` [Bug c/51294] " tortoise_74 at yahoo dot co.uk
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: tortoise_74 at yahoo dot co.uk @ 2011-11-24 11:43 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 51294
           Summary: spurious warning from -Wconversion in C and C++
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: tortoise_74@yahoo.co.uk


The code below produces a spurious warning when compiled with -Wconversion.
0 is a const so the compiler should be able to choose the correct type. This
works with gcc 4.1 on redhat 5 but not the latest snapshot of gcc 4.7 (which I
am trialing for its c++11 support). 
I do not have any intermediate compiler versions to test with.

spurious.cpp:

class foo
{
   char bar;

   foo(bool haveBar, char bar_):
      bar(haveBar?bar_:0)
   {
   }
};

##

test.sh
#!/bin/sh

gcc --version
gcc -Wconversion -c spurious.cpp

export LD_LIBRARY_PATH=/opt/gcc4.7/lib:$LD_LIBRARY_PATH
export PATH=/opt/gcc4.7/bin:$PATH
/opt/gcc4.7/bin/gcc --version
/opt/gcc4.7/bin/gcc -Werror -Wconversion -c spurious.cpp

##

brucea@:home/brucea/spurious>./test.sh
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gcc (GCC) 4.7.0 20111119 (experimental)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

spurious.cpp: In constructor ‘foo::foo(bool, char)’:
spurious.cpp:8:25: error: conversion to ‘char’ from ‘int’ may alter its value
[-Werror=conversion]
cc1plus: all warnings being treated as errors

##

The workaround is either to disable the warning with -Wno-conversion or add
unecessary constants as below. I don't think this is acceptable.

class foo2
{
   static const char zero = 0;

   char bar;

   foo2(bool haveBar, char bar_):
      bar(haveBar?bar_:zero)
   {
   }
};

This is also a bug in the C front-end:

void foo(int haveBar, char bar_)
{
  char zuul = haveBar?bar_:0;
}


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2012-11-07 17:07 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-24 11:43 [Bug c/51294] New: spurious warning from -Wconversion in C and C++ tortoise_74 at yahoo dot co.uk
2011-11-24 11:51 ` [Bug c/51294] " tortoise_74 at yahoo dot co.uk
2011-11-24 12:45 ` redi at gcc dot gnu.org
2011-11-24 16:09 ` [Bug c/51294] spurious warning from -Wconversion in C and C++ in conditional expressions manu at gcc dot gnu.org
2011-11-24 17:58 ` tortoise_74 at yahoo dot co.uk
2011-11-24 19:09 ` redi at gcc dot gnu.org
2012-01-05 15:33 ` manu at gcc dot gnu.org
2012-03-24 10:18 ` manu at gcc dot gnu.org
2012-03-26 10:17 ` paolo.carlini at oracle dot com
2012-03-30 11:08 ` paolo.carlini at oracle dot com
2012-05-12 18:21 ` paolo.carlini at oracle dot com
2012-05-12 19:10 ` manu at gcc dot gnu.org
2012-11-07 16:59 ` manu at gcc dot gnu.org
2012-11-07 17:07 ` manu at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).