Subject: Re: __UNCONST(a)
To: <>
From: David Laight <david@l8s.co.uk>
List: tech-userlevel
Date: 07/04/2004 16:38:46
On Sat, Jul 03, 2004 at 03:28:47PM -0400, James K. Lowden wrote:
> On Thu, 1 Jul 2004 Christos Zoulas wrote:
> > On Jul 2, 10:06am, yamt@mwd.biglobe.ne.jp (YAMAMOTO Takashi) wrote:
> > 
> > | > Why cast to a specific type? We could do the same, but it seems
> > | > gratuitous.
> > | 
> > | because it's only sane way without typeof().
> > 
> > Why, (void *) matches everything.
> 
> You want to defeat the whole type system, merely to quell compiler
> warnings?  
> 
> wchar_t *ws;
> const char s[] = "hello";
> ws = __UNCONST(s);
> 
> This is good?  I realize that's not the intended context, but there's
> nothing except vigilance to prevent it from being used that way, and
> vigilance breaks down in complex situations.  
> 
> C++ uses const_cast<>, providing a type.  Seems a like a good model to
> follow.  

Providing the required type doesn't help - the compiler still can't
check that you aren't changing the type of the data.
This is one reason why I dislike using casts to remove compiler/lint
warnings - it can hide a real error.

Of course you can do things like:
#define __U(s) ((void *)(uintptr_t)(s))
#define __UNCONST(type,s) (((type)(s) - s) + (type)__U(s))

Which (probably) checks that 's' is of the require basic type.


	David

-- 
David Laight: david@l8s.co.uk