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: Andrew Haley <aph at redhat dot com>
- To: Andreas Schwab <schwab at suse dot de>
- Cc: "Dave Korn" <dave dot korn at artimi dot com>, "'Andrew Pinski'" <pinskia at physics dot uc dot edu>, "'Richard Guenther'" <richard dot guenther at gmail dot com>, "'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 19:32:40 +0100
- Subject: Re: why are we not using const?
- References: <015301c69b85$435d6e30$a501a8c0@CAM.ARTIMI.COM> <jek67059vk.fsf@sykes.suse.de>
Andreas Schwab writes:
> "Dave Korn" <dave.korn@artimi.com> writes:
>
> > But it's really legal to cast away const?
>
> All that matters is the effective type of the accessed object, see 6.5#7.
It's not clear to me that it's legal to convert (const int*) to
(int*). 6.3.2.3, Pointers, says
2 For any qualifier q, a pointer to a non-q-qualified type may be
converted to a pointer to the q-qualified version of the type; the
values stored in the original and converted pointers shall compare
equal.
It doesn't seem to me to give any permission to do the conversion the
other way. But that wouldn't make any difference to this case,
because you can still do
int i = 0;
void bar()
{
i = 2;
}
void foo (const int *pi)
{
print (*pi);
bar();
print (*pi);
}
int main()
{
foo (&i);
return 0;
}
For what it's worth: we have a lot of fields in Java that are known
never to change, and to be able to explain that to the optimizers
would be a big boost.
Andrew.