On Wed, 30 Oct 2002, Leopold Toetsch wrote:

> > Nicholas Clark suggested that we could perhaps use a union, something like
> 
> Nice idea - works for me (gcc 2.95.2)
> 
> Slightly modified - please try this (cc -Wall -Wcast-qual ..)

> static union {
>       const void * __c_ptr;
>       void * __ptr;
> } __ptr_u;

> #define const_cast(b) (__ptr_u.__c_ptr = (b), __ptr_u.__ptr)

This works too, but not as well.
 
First, although I could be wrong, static variables in possibly threaded
code always worry me.

More specifically, however, my version, which used a variable local to the
function in question, ended up getting compiled (with cc -O) to the exact
same assembly code as the version with the plain cast.  Yes, it is more to
type in those few places in the code where it's needed, but I think that's
ok because one shouldn't be throwing away 'const'-ness casually anyway.

-- 
    Andy Dougherty              [EMAIL PROTECTED]

Reply via email to