This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: const warning...
Laurent GUERBY <guerby@acm.org> writes:
| On Sat, 2003-07-05 at 08:31, Fergus Henderson wrote:
| > [...]
| > void print_squares(void) {
| > int i;
| > for (i = 0; i < 10; i++) {
| > const int sqr_i = i * i;
| > printf("%d squared is %d\n", i, sqr_i);
| > }
| > }
| >
| > If you are arguing that there is a fundamental and important difference
| > between these two code snippets, then I would beg to differ. There is
| > a difference, certainly, but I don't find it especially important.
| > While I agree that it is better style to declare variables in their
| > closest enclosing scope, I wouldn't say that it makes such a *fundamental*
| > difference to the code if you instead declare them at the top of the function.
|
| On a 5 lines subprogram it doesn't make a difference, on a 100 lines
| one with a dozen of declarations, it makes a huge difference if
| 90% of those are marked constant and put in their proper scope.
I don't know how const works in Ada, but I do know for sure that
const-correctness *is* a source of problems in C++. Once you start
putting const everywhere just because a variable happened not to be
"apparaantly" changed, you might find yourself getting into trouble.
People make living business writing series of books on the topic of
const-correctness in C++.
We have a precedence about this sort of things in the compiler, and
it turns out to be a fiasco. It is named -Weffc++.
-- Gaby