Const warning? (was: Re: [Patch] More redundant...)

Robert Dewar dewar@gnat.com
Tue Apr 22 00:07:00 GMT 2003


> 
> I'm glad to hear that there may already be a patch to have a "const" 
> warning!  I can certainly see that pointers to pointers to pointers etc. 
> would be more difficult to do, especially since const can be at any level.
> 
> To me, it is certainly worth having a warning if it can result in faster 
> compile times and/or better code being generated.
> 
> As Paolo says, ideally the compiler would be able to generate the same 
> code whether variables are marked const or not.  However, this is maybe 
> not realistic.  So maybe it would just be a good idea to encourage use 
> of const to save the compiler from having to do all the analysis to 
> determine that a variable is const.
> 


Two points. First of all, I think the warning is very valuable even if it
does not cover the pointer case fully (don't let best be the enemy of
good). Most certainly we have found this valuable in Ada.

Second, I think the issue of better code is besides the point. It contributes
significantly to the readability and maintainability of code to label 
constant objects as constant, since you do not have to worry about the
possibility of them being changed. So this is something that should be
done as a matter of style. Since for a long time C lacked this capability,
C (and C++) programmers are not in the habit of designated things as
constant when they should, and a nudge from the compiler is helpful.



More information about the Gcc mailing list