Subject: Re: toolchain/22118: make won't compile with -Wcast-qual
To: David Laight <david@l8s.co.uk>
From: Richard Earnshaw <rearnsha@arm.com>
List: tech-toolchain
Date: 07/16/2003 15:53:16
> > C++ has const_cast for these circumstances (explicitly and safely casting 
> > away const-ness).  Unfortunately, C has no direct equivalent.
> 
> ISTM that these gcc warnings are OTT, or is C99 just plain broken?
> Surely the C way to remove 'const' is just a cast?
> 

Yes, but C doesn't really have a way to distinguish between:


typedef XXX foo;

const foo *bar;

func()
{
...
	wibble((foo *)bar);
}

and


	wombat((int *)bar);

The former simply casts away const-ness, while maintaining the pointer 
type.  The latter discards const-ness and changes the type.

I could see an argument for avoiding warning on the former, but the latter 
probably represents a bug (if you really mean it, then apply two casts to 
make it clear):

	wombat((int *) (foo *) bar); /* Drop const *and* convert.  */ 

> These sort of casts (along with (void) casts of ignored function results)
> just make code unreadable.
> 

I agree with that one to some extent, but some folk argue that return 
types should always be checked  -- sometimes having exceptions would be 
useful...

R.