This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH]: Fix problematic -Wcast-qual cases using new CONST_CAST macro
On 7/27/07, Kaveh R. GHAZI <ghazi@caip.rutgers.edu> wrote:
> On Fri, 27 Jul 2007, Richard Guenther wrote:
>
> > On 7/27/07, Kaveh R. GHAZI <ghazi@caip.rutgers.edu> 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.