Re: Variables in for loop (style issue)
James Dennett wrote:
I've shown above that you cannot "just omit it".
#include <iostream>
#include <ostream>
void print(int r) { std::cout << r << '\n'; }
template <int i> class foo {};
int main()
{
print(3);
#define array_size 4
int array[array_size];
foo<array_size> object;
}
Compiles and works fine. If you object to using a macro, I can suggest:
enum { array_size = 4 };
Or I can argue that we're talking about const as a type qualifier, not
const as a storage class.
const is more optional in C, but this is a C++ newsgroup, and const
(and the rest of static type checking) is more key to C++ than it is
to C.
Referring to your example, I certainly agree that const is key to making:
const int array_size(4);
const declarations work, but I think that's more self-referential than
key <g>. As for initializing references with integer literals, that's
more filling in an odd corner than key. (There's no reason to prefer:
void print(const int& r)
over:
void print(int r)
that I can think of.) That aside, static type checking is just as strong
in C as in C++, so I don't see how static type checking is "more key to
C++". (*)
-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
(*) Function prototypes are optional in C - but are specifically
declared "obsolescent" in C99.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]