使用C void 参数“void foo(void)"更好吗?或者不是“void foo()"? [英] Is it better to use C void arguments "void foo(void)" or not "void foo()"?
问题描述
哪个更好:void foo()
或 void foo(void)
?使用 void 它看起来丑陋且不一致,但有人告诉我它很好.这是真的吗?
What is better: void foo()
or void foo(void)
?
With void it looks ugly and inconsistent, but I've been told that it is good. Is this true?
我知道一些旧的编译器会做一些奇怪的事情,但是如果我只使用 GCC,void foo()
好吗?foo(bar);
那么会被接受吗?
I know some old compilers do weird things, but if I'm using just GCC, is void foo()
Ok? Will foo(bar);
then be accepted?
推荐答案
void foo(void);
这是在 C 中说无参数"的正确方式,在 C++ 中也适用.
That is the correct way to say "no parameters" in C, and it also works in C++.
但是:
void foo();
在 C 和 C++ 中意味着不同的东西!在 C 中,它的意思是可以接受任意数量的未知类型的参数",在 C++ 中,它的含义与 foo(void)
相同.
Means different things in C and C++! In C it means "could take any number of parameters of unknown types", and in C++ it means the same as foo(void)
.
可变参数列表函数本质上是非类型安全的,应尽可能避免使用.
Variable argument list functions are inherently un-typesafe and should be avoided where possible.
这篇关于使用C void 参数“void foo(void)"更好吗?或者不是“void foo()"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!