public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* -Wcast-qual and casting away
@ 2009-05-21 11:50 Ian Lance Taylor
  2009-05-21 11:58 ` Richard Guenther
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Ian Lance Taylor @ 2009-05-21 11:50 UTC (permalink / raw)
  To: gcc

Consider this C/C++ program:

extern void **f1();
void f2(const char *p) { *(const void **)f1() = p; }

If I compile this program with g++ -Wcast-qual, I get this:

foo2.cc:2: warning: cast from type ‘void**’ to type ‘const void**’ casts away qualifiers

If I compile this program with gcc -Wcast-qual, I do not get any
warning.

Let's overlook the fact that the text of the g++ warning does not make
any sense--I am certainly not casting anything away.  The warning is
conceptually plausible for the same reason that you can't assign a
char** variable to a const char** variable without a cast.  At least, I
think one could make a argument that that is so.  But it's not a *very*
strong argument, as -Wcast-qual is documented to warn about cases where
a type qualifier is removed, and that is manifestly not happening here.
-Wcast-qual is useful to catch certain programming errors; I don't think
anybody adding a const qualifier is actually making a mistake.

All that aside, I can't think of any reason that the C and C++ frontends
should be different in this regard.  Does anybody want to make an
argument for which of these choices we should adopt?

1) Keep things the same: the C++ frontend warns, the C frontend doesn't.
   Consistency is overrated.

2) Change the C frontend to also warn about this case, albeit with a
   better message.

3) Change the C++ frontend to not warn about this case.

Of course in all cases the frontends should continue to warn about a
cast from const void** to void**.

Ian

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

end of thread, other threads:[~2009-05-21 17:36 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-21 11:50 -Wcast-qual and casting away Ian Lance Taylor
2009-05-21 11:58 ` Richard Guenther
2009-05-22  0:20   ` Gabriel Dos Reis
2009-05-21 12:09 ` Andreas Schwab
2009-05-21 12:50   ` Richard Guenther
2009-05-21 15:13     ` Andreas Schwab
2009-05-21 15:22     ` Sebastian Redl
2009-05-21 22:48     ` Ian Lance Taylor
2009-05-22  1:01       ` Gabriel Dos Reis
2009-05-21 12:38 ` Joseph S. Myers
2009-05-21 17:36   ` Ian Lance Taylor
2009-05-21 15:26 ` Dave Korn
2009-05-22  0:16 ` Gabriel Dos Reis

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).