From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3864 invoked by alias); 21 May 2009 17:31:53 -0000 Received: (qmail 3851 invoked by uid 22791); 21 May 2009 17:31:52 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00,SARE_MSGID_LONG40,SPF_PASS,WEIRD_PORT X-Spam-Check-By: sourceware.org Received: from mail-qy0-f126.google.com (HELO mail-qy0-f126.google.com) (209.85.221.126) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 21 May 2009 17:31:48 +0000 Received: by qyk32 with SMTP id 32so1788947qyk.0 for ; Thu, 21 May 2009 10:31:46 -0700 (PDT) MIME-Version: 1.0 Received: by 10.220.74.65 with SMTP id t1mr3089904vcj.89.1242927106116; Thu, 21 May 2009 10:31:46 -0700 (PDT) Reply-To: gdr@integrable-solutions.net In-Reply-To: <84fc9c000905210320g7525cb8cob268133e82896ec1@mail.gmail.com> References: <84fc9c000905210320g7525cb8cob268133e82896ec1@mail.gmail.com> Date: Fri, 22 May 2009 00:20:00 -0000 Message-ID: <206fcf960905211031r51490670td2d00857dab46c83@mail.gmail.com> Subject: Re: -Wcast-qual and casting away From: Gabriel Dos Reis To: Richard Guenther Cc: Ian Lance Taylor , gcc@gcc.gnu.org Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2009-05/txt/msg00556.txt.bz2 On Thu, May 21, 2009 at 5:20 AM, Richard Guenther wrote: > On Thu, May 21, 2009 at 7:10 AM, Ian Lance Taylor wrote: >> Consider this C/C++ program: >> >> extern void **f1(); >> void f2(const char *p) { *(const void **)f1() =3D p; } >> >> If I compile this program with g++ -Wcast-qual, I get this: >> >> foo2.cc:2: warning: cast from type =91void**=92 to type =91const void**= =92 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. =A0The warning is >> conceptually plausible for the same reason that you can't assign a >> char** variable to a const char** variable without a cast. =A0At least, I >> think one could make a argument that that is so. =A0But 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. =A0Does 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. >> =A0 Consistency is overrated. >> >> 2) Change the C frontend to also warn about this case, albeit with a >> =A0 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**. > > As the C++ warning doesn't make any sense I vote for 3). Why do you think it the warning does not make sense? The only you can go from void** to const void** is that you actually remove the const in the middle of const void* const *. That is precisely what the cast is doing -- so the warning is legitimate. -- Gaby