I\'ve seen methods with the following signature:
void foo (void);
They take no argument, however I\'m wondering whether doing this is usefu
This is a legacy from the older versions of C
for functions with no arguments
In C++ code there is no reason whatsoever to use void in this way. What's more it is very much not the idiomatic way to declare parameterless functions.
The C++03 standard says (emphasis mine):
8.3.5.2
The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [Note: the parameter-declaration-clause is used to convert the arguments specified on the function call; see 5.2.2. ] If the parameter-declaration-clause is empty, the function takes no arguments.
This means that if you are talking to the compiler it's just a matter of taste.
If you are writing code that will be read by others, then the C++ way of doing things is
void foo();
The other form remains valid only for reasons of compatibility with C, where there was a difference among the two signatures.
This is a holdover from older versions of C, where foo()
meant "a function with an unknown number of parameters" and foo(void)
means "a function with zero parameters." In C++, foo()
and foo(void)
both mean "a function with zero parameters", but some people prefer the second form because it is more explicit.