C distinguishes between a function declared with an empty parameter list and a function declared with a parameter list consisting of onlyvoid. The former is an unprototyped function taking an unspecified number of arguments, while the latter is a prototyped function taking no arguments.
// C code
extern int foo(); // Unspecified parameters
extern int bar(void); // No parameters
void baz()
{
foo(0); // Valid C, invalid C++
foo(1, 2); // Valid C, invalid C++
bar(); // Okay in both C and C++
bar(1); // Error in both C and C++
}
C++, on the other hand, makes no distinction between the two declarations and considers them both to mean a function taking no arguments.
// C++ code
extern int xyz();
extern int xyz(void); // Same as 'xyz()' in C++,
// Different and invalid in C For code that is intended to be compiled as either C or C++, the best solution to this problem is to always declare functions taking no parameters with an explicit
void prototype. For example:
// Compiles as both C and C++
int bosho(void)
{
...
} Empty function prototypes are a deprecated feature in C99 (as they were in C89).
Refer to http://david.tribble.com/text/cdiffs.htm
本文探讨了C和C++中空参数列表与void参数列表的区别:前者表示未指定参数数量的函数,后者表示不接受任何参数的函数。此外,文章还讨论了这两种声明方式在两种语言中的行为差异,并提供了最佳实践建议。
1057

被折叠的 条评论
为什么被折叠?



