This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: why are we not using const?
- From: "Richard Guenther" <richard dot guenther at gmail dot com>
- To: "Dave Korn" <dave dot korn at artimi dot com>
- Cc: "Andrew Pinski" <pinskia at physics dot uc dot edu>, "Kaveh R. Ghazi" <ghazi at caipclassic dot rutgers dot edu>, gdr at integrable-solutions dot net, gcc at gnu dot org, lopezibanez at gmail dot com
- Date: Thu, 29 Jun 2006 16:12:32 +0200
- Subject: Re: why are we not using const?
- References: <78624A45-B2A5-458C-907C-3C8DF871EA4D@physics.uc.edu> <015301c69b85$435d6e30$a501a8c0@CAM.ARTIMI.COM>
On 6/29/06, Dave Korn <dave.korn@artimi.com> wrote:
On 29 June 2006 14:55, Andrew Pinski wrote:
> On Jun 29, 2006, at 9:51 AM, Dave Korn wrote:
>
>> That's cheating! You casted away const, it's a blatant aliasing
>> violation, you deserve everything you get.
>
> No it is not, in fact it is legal C and there is no aliasing
> violation as you
> are still accessing the memory as an "int".
Sorry, 'aliasing' was the wrong choice of word. But it's really legal to
cast away const? I'm boggled. In that case it simply might as well not exist
at all; it conveys no usable semantic information. I would argue that const
should be removed from the language if this is the case; if all you want is to
document the properties of function args, it makes more sense to do it with
macros that #define to nothing, like M$ does with 'IN', 'OUT', 'OPTIONAL' and
so on in all their header files.
;)
It's useful for qualifiers on the actual object like
const int foo = 1;
only for pointer targets it's useless but "needed" to form &foo, which
is const int*. And it is undefined if you try to modify foo via
int * bar = (int*)&foo;
*bar = 1;
so, it's only the CV qualifier of the object that matters, not the one
you stick to some arbitrary pointer to an object.
The solution is of course to teach the middle-end a new concept
of pointer to non-modifiable memory and do some language extension
to use it. Maybe there even are languages that have a useful const
concept (like C++ for the this object on const methods).
Richard.