Consider these two function definitions:
void foo() { }
void foo(void) { }
Is there any difference between these two? If not, why is the void
argument there? Aesthetic reasons?
Consider these two function definitions:
void foo() { }
void foo(void) { }
Is there any difference between these two? If not, why is the void
argument there? Aesthetic reasons?
Historical note: this answer applies to C17 and older editions. C23 and later editions treat void foo()
differently.
In C:
void foo()
means "a function foo
taking an unspecified number of arguments of unspecified type"void foo(void)
means "a function foo
taking no arguments"In C++:
void foo()
means "a function foo
taking no arguments"void foo(void)
means "a function foo
taking no arguments"By writing foo(void)
, therefore, we achieve the same interpretation across both languages and make our headers multilingual (though we usually need to do some more things to the headers to make them truly cross-language; namely, wrap them in an extern "C"
if we're compiling C++).
void
, then it could have avoided the "most vexing parse" problem. –
Moonset void foo()
was the only syntax to declare a function. When signatures where introduced, the C committee had to disambiguate the no-parameter from the old syntax, and introduced the void foo(void)
syntax. C++ took it for the sake of compatibility. –
Bothnia void foo()
instead of void foo(void)
will produce a functional difference? I.e. I have been using the version without the void for many years and havent seen any problem, am I missing something? –
Utilitarian std::vector<int> v(std::vector<int>());
or any combination of 2 types, constructing one directly in the constructor of the other. –
Permittivity void foo() { if ( rand() ) foo(5); }
compiles and runs (causing undefined behaviour unless you're very lucky), whereas void foo(void)
with the same body would cause a compilation error. –
Sparklesparkler extern "C"
around the C names. –
Textile if (getchar() == 'x') 1/0;
, the behaviour is only undefined if execution reaches this line, and certain input is given. –
Sparklesparkler rand()
; if it happens to return 0
then there is no undefined behaviour: having foo(5)
in code that is never executed does not imply the program has UB. I'm not conflating UB with symptoms of UB. Maybe the getchar()
example is better than the rand()
example to demonstrate this point in order to avoid arguments about guarantees of the sequence of numbers generated by rand()
–
Sparklesparkler I realize your question pertains to C++, but when it comes to C the answer can be found in K&R, pages 72-73:
Furthermore, if a function declaration does not include arguments, as in
double atof();
that too is taken to mean that nothing is to be assumed about the arguments of atof; all parameter checking is turned off. This special meaning of the empty argument list is intended to permit older C programs to compile with new compilers. But it's a bad idea to use it with new programs. If the function takes arguments, declare them; if it takes no arguments, use void.
C++11 N3337 standard draft
There is no difference.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
Annex C "Compatibility" C.1.7 Clause 8: declarators says:
8.3.5 Change: In C ++ , a function declared with an empty parameter list takes no arguments. In C, an empty parameter list means that the number and type of the function arguments are unknown.
Example:
int f(); // means int f(void) in C ++ // int f( unknown ) in C
Rationale: This is to avoid erroneous function calls (i.e., function calls with the wrong number or type of arguments).
Effect on original feature: Change to semantics of well-defined feature. This feature was marked as “obsolescent” in C.
8.5.3 functions says:
4. The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [...] If the parameter-declaration-clause is empty, the function takes no arguments. The parameter list (void) is equivalent to the empty parameter list.
C99
As mentioned by C++11, int f()
specifies nothing about the arguments, and is obsolescent.
It can either lead to working code or UB.
I have interpreted the C99 standard in detail at: https://mcmap.net/q/14687/-is-it-better-to-use-c-void-arguments-quot-void-foo-void-quot-or-not-quot-void-foo-quot-duplicate
As of C23, C has adopted C++'s semantics for function declarations with empty parameter lists.
In C++ (all editions) and C (C23 and later):
void foo()
means "a function foo
taking no arguments".void foo(void)
means "a function foo
taking no arguments".Non-prototype function declarations (which is what void foo()
used to be) have been removed from the C language.
This change was introduced by the proposal N2841: No function declarators without prototypes, which has the summary:
This removes the obsolescent support for function declarators without prototypes. The old syntax for function declarators without prototypes is instead given the C++ semantics.
Quoting the current C23 standard draft (N3096, Annex M.2 Fifth Edition):
Major changes in this fifth edition (
__STDC_VERSION__
202311L
) include
...
- mandated function declarations whose parameter list is empty be treated the same as a parameter list which only contain a single
void;
In both C and C++, void foo()
and void foo(void)
are now exactly equivalent, and mean "a function taking no arguments."
()
with (void)
. But as of C23, ()
is once again fine to use. And those who've lived underneath a rock for 25 years need not worry either. –
Copenhaver int main(void) { /* ... */ }
and int main() { /* ... */ }
or make a note about it, as per the other case. –
Galvano In C, you use a void in an empty function reference so that the compiler has a prototype, and that prototype has "no arguments". In C++, you don't have to tell the compiler that you have a prototype because you can't leave out the prototype.
© 2022 - 2024 — McMap. All rights reserved.