warning on an empty parameter list in function declaration

Started by
15 comments, last by exwonder 16 years, 1 month ago
I'm looking to find if there's a way to raise a warning when a function is declared, in C, with an empty parameter list. I'm trying to detect when I don't put a 'void' in the function declaration of a function that has an empty parameter list. Is this possible?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
If -Wall -ansi -pedantic doesn't catch it, then there isn't a whole lot else you can do. I do wonder why you would care in particular - i don't recall empty parameter lists being exploited very often.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Good programming practice. One of the more senior programmers at work mentioned it and I just wondered if it were possible.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.

I find the practice of putting void in the parameter list equatable to keeping $0 bills in your wallet on days when you're not shopping.

It's not good programming practice. At best, it's stylistic.
Quote:Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...


Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

Just another reason why C is quite disappointing as a language.
Quote:Original post by Rydinare
Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

Just another reason why C is quite disappointing as a language.


C++ is no better really. You get similar behavior with throw specifications (a function that does not specify any thrown exceptions can throw any, you have to explicitly specify that a function will throw no excpetions).
Quote:Original post by Rydinare
Quote:Original post by swiftcoder
Quote:Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...


Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

Just another reason why C is quite disappointing as a language.


C was designed as a light layer on top of assembly which it does a great job of accomplishing.
Quote:Original post by Rydinare
Ahh. I didn't realize that C treated it differently. That explains where the usage of foo(void) came from. Even so, that's very peculiar that they would do something so silly, when there's no way to access them (or is there?).

In C function declarations don't necessarily match function definitions 100%. You can declare a function

int foo();

and later define it as

int foo(int bar);

and then hope and pray that whenever foo() is called, they use a single function parameter. In more practical terms, this can be used to bind functions with different parameter types into function pointers. Common examples in lex/yacc bind functions like sqrt() and pow() (which take different number of arguments) into the same function pointer type and then use parse trees to determine how many arguments to pass to the function pointers.
Quote:Original post by swiftcoder
Quote:Original post by Rydinare
What exactly is the keyword void in a parameter list buying you? Is it not more intuitive when there's no parameters for the parameter list to actually be empty? I never understood that.


In standard C (as opposed to C++), no parameters to a function actually indicates an arbitrary number of arguments, IIRC. How one is supposed to access those arguments I never determined...


Not 100% sure but probably the same way the ... access variables.

This topic is closed to new replies.

Advertisement