From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18508 invoked by alias); 27 Jul 2007 18:38:31 -0000 Received: (qmail 18495 invoked by uid 22791); 27 Jul 2007 18:38:31 -0000 X-Spam-Check-By: sourceware.org Received: from py-out-1112.google.com (HELO py-out-1112.google.com) (64.233.166.181) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 27 Jul 2007 18:38:29 +0000 Received: by py-out-1112.google.com with SMTP id a29so2127319pyi for ; Fri, 27 Jul 2007 11:38:27 -0700 (PDT) Received: by 10.64.148.8 with SMTP id v8mr5528918qbd.1185561507090; Fri, 27 Jul 2007 11:38:27 -0700 (PDT) Received: by 10.65.240.11 with HTTP; Fri, 27 Jul 2007 11:38:27 -0700 (PDT) Message-ID: <84fc9c000707271138u6171f592x3f69800f12106a0a@mail.gmail.com> Date: Fri, 27 Jul 2007 19:32:00 -0000 From: "Richard Guenther" To: "Kaveh R. GHAZI" Subject: Re: [PATCH]: Fix problematic -Wcast-qual cases using new CONST_CAST macro Cc: gcc-patches@gcc.gnu.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <84fc9c000707270219i4702de61y70899cd7e3349d72@mail.gmail.com> X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org X-SW-Source: 2007-07/txt/msg02006.txt.bz2 On 7/27/07, Kaveh R. GHAZI wrote: > On Fri, 27 Jul 2007, Richard Guenther wrote: > > > On 7/27/07, Kaveh R. GHAZI wrote: > > > +union gcc_constcast > > > +{ > > > + const void *cv; > > > + void *v; > > > +}; > > > +#define CONST_CAST(X) ((__extension__(union gcc_constcast)(const void *)(X)).v) > > > > Can you explain in how this is better than using an explicit cast to the > > non-const version like in memcpy ((char *)x, ...)? > > Sure. Besides my main point which is silencing -Wcast-qual warnings, the > CONST_CAST macro is grep-able. I.e. you can search for the few places > where it's used and audit if they are correctly applied. You cannot do > that with regular C-style casts. Another reason is that with C-style > casts there isn't any indication of why it's there. The CONST_CAST macro > comments itself to someone reading the code. (These are some of the > rationales for the creation of C++'s cast operators.) The macro isn't > type-safe like the C++ operator is, but it's a poor man's C option. :-) Ha! Maybe a reason to switch to C++ ;) How about #ifdef __cplusplus #define CONST_CAST(x) .. template magic follows .. #else ... #endif ? ;) Richard.