D
David Brown
Sorry, but anything that involves doing X, Y or Z to make type specs 'easy'
isn't a solution! It shouldn't be necessary to do anything; they should be
self-explanatory. However if you are stuck with writing actual C (I'm not)
then I suppose you might need to use these methods.
Why should it be so easy to write a complex type definition as a single
statement? There is nothing wrong with using typedefs - just as there
is nothing wrong with using multiple lines of code when you /could/ get
the same effect from one line, or using extra temporary variables to
make code clearer.
(But I would probably still never use 'const' anyway.)
Why not? "const" is a very useful keyword in C. It helps catch
mistakes, and lets the compiler generate more efficient code - sometimes
significantly so. If you are not going to change something, why not let
the compiler know that so that it can help prevent accidents and take
advantage of the knowledge in code generation.
For the most part, "static const int sizeMax = 1024;" does the same
job as
"enum { sizeMax = 1024 };" or "#define sizeMax 1024", but it does so with
safer typing, better static error checking, more flexibility and - IMHO -
clearer code. The only exception is that you can't use the "static
const"
for the size of statically allocated arrays. It's a painful omission
from
the standards.Of course, it is possible to write things in reverse:
static char buffer[1024];
static const int sizeMax = sizeof(buffer) / sizeof(buffer[0])Whether or not you think that is better is a matter of opinion, but it's
an alternative that avoids the #define or the somewhat unnatural enum,
and
still avoids using the "magic number" twice in the code.
But, sizeMax is still, as far as I can gather, some value with reserved
storage, that you can take the address of. And you can't declare another
array using the same size as you say.
"static const" objects typically don't require storage - the compiler
knows they will never be exported or visible outside the compilation
unit (unless you take their address, of course), so it doesn't need to
make storage space. An object such as "static const int sizeMax =
1024;" is likely to generate exactly the same code in use as a #define.
You are correct that you can't declare another array of the same size -
that's the limitation of static consts.
So it still falls short of what you can do with a proper named constant.
It is closer than you seem to think, but it still falls short.
You're right that it's another thing that would have been very easy to get
right, but forty years on and the language still doesn't have a plain,
straightforward way of declaring a named constant!
C++ lets you use static const in this way (and non-static const too).
So I don't see why it's a problem for C.
(It's a little crazy really; I have now several language projects where I
translate source code that has 'proper' named constants, into actual C. Did
I use #defines, or enums? Both have limitations to do with scope and type.
But I've just checked and the solution I came up with was the following; if
the source uses this (made C-like so as not to frighten anyone):
const sizeMax = 1024 /* 'int' is optional */
"int" is not optional in well-written code, IMHO.
char buffer[sizeMax]
int i=sizeMax
The generated C is just this:
char buffer[1024];
int i=1024;
the problem simply disappears! It's almost a non-issue, unless you have
to write actual C.)