I
Ian Collins
So why not do it properly and use a static analyser?
We didn't have one (this was early 90s), the C++ compiler was our static
analyser!
So why not do it properly and use a static analyser?
Ian Collins <ian- said:We didn't have one (this was early 90s), the C++ compiler was our
static analyser!
Static analysers for C have been around since the late 1970's
There were some very good C90 static analysers from about 1991.
BTW had C++ even been standardised at that time?
No.
A compiler translates code. It is NOT a static analyser.
So what was your excuse again?
enums should have their own namespace, as wellWell a huge number of bugs in that embedded code base were caused by
wrong parameter types (mostly enums) being passed around. The C++
compiler was an ideal tool to flush those out. Once those were removed,
our lives were much more enjoyable. Then there was the testing I was
able to do once I could run the code on my desktop...
BGB said:they are functionally equivalent in that both can be called without
any arguments and will exhibit the same behavior.
there will be a difference at which point one calls the function with
arguments (in one case the compiler will reject the code making the
call and in the other it wont). however, if one assumes that the code
is in-error (by calling a function which doesn't take arguments with
an arguments list), then this doesn't result in a behavioral
difference in the code to be run.
yes, it does do this, but this is not much different than passing or
not passing "-Werror-implicit-function-declaration" to GCC.
Malcolm McLean said:enums should have their own namespace, as well
enum states { ALABAMA ... WASHINGTON ...};
enum presidents {WASHINGTON ... OBAMA };
is an unexceptional thing to want to do.
Malcolm McLean said:enums should have their own namespace, as well
enum states { ALABAMA ... WASHINGTON ...};
enum presidents {WASHINGTON ... OBAMA };
is an unexceptional thing to want to do.
enums should have their own namespace, as well
enum states { ALABAMA ... WASHINGTON ...};
enum presidents {WASHINGTON ... OBAMA };
is an unexceptional thing to want to do.
If you assign an enum to a variable of another type or use it in anThat's only the start. How would one determine which WASHINGTON is
intended? It's easy in languages like ML with type inference (or in
strongly typed languages like Ada) but it's problematic in C.
So you really are banking up problems!
One of the original reasons I put a C project through a C++ compiler was
to spot the (considerable number) of functions being called with the
wrong parameters.
I really really wish K&R style prototypes were removed completely from C.
I think that providing a prototype for error checking calls is important
to the extent that () and (void) are not functionally equivalent, but
you may define the term how you like. The difference in the semantics
is important, the term used to describe the similarities is not.
How could it be similar? The implicit-function-declaration warning (or
error) applies only to code where there is neither a declaration nor a
definition in scope, i.e. where neither the () or the (void) form has
been used (yet).
BTW, a C99 compiler *must* complain about the use of a function with no
declaration, but since both the () and (void) forms constitute a
declaration that has no bearing on this issue.
it is generally fairly well established that things can be substituted
for things which are internally or formally very different without
effecting the observable behavior, and are hence interchangeable.
"if it looks like a duck and quacks like a duck, it is a duck".
provided one calls the function as:
"foo();" then whether it is declared with "()" or "(void)" doesn't make
a whole lot of difference, and the case where one calls "foo(bar);" can
be ignored as it falls outside of the established scope (calling a
no-argument function).
No, I mean the opposite.That does not answer the question I asked. Which WASHINGTON is this?
You had only two (with different values) so I suppose that the cast
could be taken by the compiler to mean that the "other" WASHINGTON was
intended but that's a peculiar rule and does not scale. If there are
three enums that define WASHINGTON, which one gets converted here?
That's a nuisance, yes. Ideally we'd like to be able to define "area"However this should be relatively rare.
Unless you change lots of other rules I suspect it will be quite
common. Consider 'area[WASHINGTON]' for example. You'd have to
re-define the integer promotions and where they occur.
Not really. Struct members also have their own namespace, and structsThat's giving a unique namespace to *every* enum. and strongly typing
any variables holding them. It's a *really* big change to the language..
Malcolm McLean said:If you assign an enum to a variable of another type or use it in an
expression you'd have to cast it.
int fortran_state_index = (state) WASHINGTON + 1;
However this should be relatively rare.
BGB said:no, the "()" lines are mostly non-argument functions, just the tendency
is to use "()" rather than "(void)", mostly for aesthetic reasons.
but, in all, only a small percentage of the functions around are
no-argument functions (as in, lack a parameter list), hence, no big issue...
459 out of 15281 prototypes is hardly a large number...
Malcolm McLean said:Not really.
Struct members also have their own namespace, and structs
are strongly typed.
The problem is getting the syntax right.
I've addressed this in your followup.
So you are defining the scope of this discussion to be only correct
programs? That discussion does not interest me. You can use ()
everywhere in correct programs and everything will work. Presumably you
use prototypes in other cases but you seem to have decided that the
checks you otherwise like to have don't matter for no-argument
functions. I don't see why.
Except that here we have the chance of being told about a problem with
the simplest possible change in the source. That's a big win for me.
Very few compilers are conforming without options being set.
Seems different enough to me to be important.
BGB said:prototypes offer a unique power:
they make float and double arguments and return values work correctly;
they make pointer arguments and return values work correctly on some
systems (such as x86-64);
...
for example, if no prototype is used:
the compiler will automatically promote any float arguments to double;
Right.
any return value will be assumed to be "int", regardless of whatever is
there, which leads to bad results if either the argument if a float or
double (and thus goes into a different CPU register or similar), or
"sizeof(void *)!=sizeof(int)", or if a different CPU register is used
(say, for int and pointer returns).
Malcolm McLean said:No, I mean the opposite.
In C++ we would write state::WASHINGTON. (state) WASHINGTON is more C-
like,
because
state BillGateseshangout = WASHINGTON;
and
president thatchapwiththefunnywig = WASHINGTON;
are both fine.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.