[PATCH INSTALLED]: delete some const shadow functions

Richard Guenther richard.guenther@gmail.com
Sat Sep 1 20:23:00 GMT 2007


On 9/1/07, Kaveh R. GHAZI <ghazi@caip.rutgers.edu> wrote:
> On Sat, 1 Sep 2007, Diego Novillo wrote:
>
> > I do not care for CONST_CAST either.  It's also hideous and offers no
> > benefits.  However, I guess I can learn to tolerate it.
>
> Gee thanks. :-/ I agree the implementation using the union trick is a
> hack, but that's hidden inside a macro as are many ugly things such as
> this.  (See again the checking macros example).  That union option is the
> only thing I can think of that works.  Using CONST_CAST inside code is no
> worse than C++'s const_cast operator.  The general benefits of a cast
> operator (or macro in the case of C) I've discussed already in the
> CONST_CAST patch thread.
>
> Beyond the general benefits of a cast operator, the specific benefit to
> GCC of using CONST_CAST is that there are cases where fixing -Wcast-qual
> warnings is impossible, and CONST_CAST allows a human to look at the code
> in question and overrule the compiler.  Think of it as analogous to the
> __extension__ keyword on -pedantic warnings.  Examples of the impossible
> cases are listed in the comment above the macro definition.

Now, the CONST_CAST macro as is is worse than the C++ counterpart.
For const_cast<> you have to specify the type you cast to while for
CONST_CAST you just magically strip it by casting to void*.  Which
leads to the following only possibility to implement it with C++...

#if defined(__cplusplus)
template <class T>
struct no_const_trait { typedef T Type; };
template <class T>
struct no_const_trait<const T *> { typedef T* Type; };
template <class T>
typename no_const_trait<T>::Type CONST_CAST (T x)
 { return const_cast<typename no_const_trait<T>::Type>(x); }
#else
...
#endif

not exactly the nicest variant.

Richard.



More information about the Gcc-patches mailing list