C++ 中的 foo(void) 和 foo() 之间有区别吗? 还是C?
考虑这两个函数定义:
void foo() { }
void foo(void) { }
这两个之间有什么区别吗? 如果没有,为什么会有 void
参数? 审美原因?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 C 中,您在空函数引用中使用 void ,以便编译器具有原型,并且该原型“无参数”。 在 C++ 中,您不必告诉编译器您有原型,因为您不能忽略原型。
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.
从 C23 开始,C 已采用 C++ 的语义来声明具有空参数列表的函数。
在 C++(所有版本)和 C(C23 及更高版本)中:
void foo()
表示“不带参数的函数foo
”。void foo(void)
表示“不带参数的函数foo
”。非原型函数声明(这就是
void foo()
曾经)已从 C 语言中删除。此更改是由提案 N2841:无函数声明符引入的没有原型,其中有摘要:
引用当前的C23标准草案(N3096,附件M.2第五版):
在 C 和 C++ 中,
void foo()
和void foo(void)
现在完全等价,意思是“不带参数的函数”。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 functionfoo
taking no arguments".void foo(void)
means "a functionfoo
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:
Quoting the current C23 standard draft (N3096, Annex M.2 Fifth Edition):
In both C and C++,
void foo()
andvoid foo(void)
are now exactly equivalent, and mean "a function taking no arguments."C++11 N3337标准草案
没有区别。
http://www.open-std。 org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
附件 C“兼容性”C.1.7 第 8 条:声明符 说:
8.5.3 函数 说:
C99
正如 C++11 所提到的,
int f()
没有指定任何有关参数的内容,并且已过时。它可以导致工作代码或 UB。
我详细解释了C99标准:https://stackoverflow.com/a/36292431/895245
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.5.3 functions says:
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://stackoverflow.com/a/36292431/895245
我知道你的问题与 C++ 有关,但是当涉及到 C 时,答案可以在 K&R< 中找到/a>,第 72-73 页:
I realize your question pertains to C++, but when it comes to C the answer can be found in K&R, pages 72-73:
历史记录:此答案适用于 C17 及更早版本。 C23 及更高版本以不同方式对待
void foo()
。在C中:
void foo()
表示“一个函数foo
采用未指定数量的未指定类型的参数”void foo(void )
表示“不带参数的函数foo
”在 C++ 中:
void foo()
表示“函数foo”
不带参数”void foo(void)
表示“一个不带参数的函数foo
”通过编写
foo(void)
,因此,我们在两种语言中实现了相同的解释,并使我们的标头成为多语言的(尽管我们通常需要对标头做更多的事情以使它们真正跨语言;即将它们包装在extern "C"< /code> 如果我们正在编译 C++)。
Historical note: this answer applies to C17 and older editions. C23 and later editions treat
void foo()
differently.In C:
void foo()
means "a functionfoo
taking an unspecified number of arguments of unspecified type"void foo(void)
means "a functionfoo
taking no arguments"In C++:
void foo()
means "a functionfoo
taking no arguments"void foo(void)
means "a functionfoo
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 anextern "C"
if we're compiling C++).