C++ 中的 foo(void) 和 foo() 之间有区别吗? 还是C?

发布于 2024-07-04 03:31:16 字数 142 浏览 4 评论 0原文

考虑这两个函数定义:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独守阴晴ぅ圆缺 2024-07-11 03:31:16

在 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.

我很OK 2024-07-11 03:31:16

从 C23 开始,C 已采用 C++ 的语义来声明具有空参数列表的函数。

在 C++(所有版本)和 C(C23 及更高版本)中:

  • void foo() 表示“不带参数的函数 foo”。
  • void foo(void) 表示“不带参数的函数 foo”。

非原型函数声明(这就是 void foo() 曾经已从 C 语言中删除

此更改是由提案 N2841:无函数声明符引入的没有原型,其中有摘要:

这消除了对没有原型的函数声明符的过时​​支持。 没有原型的函数声明符的旧语法被赋予了 C++ 语义

引用当前的C23标准草案(N3096,附件M.2第五版):

第五版 (__STDC_VERSION__ 202311L) 中的主要更改包括
...

  • 参数列表为空的强制函数声明将被视为与
    仅包含单个 void;
  • 的参数列表

在 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 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."

墨落画卷 2024-07-11 03:31:16

C++11 N3337标准草案

没有区别。

http://www.open-std。 org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf

附件 C“兼容性”C.1.7 第 8 条:声明符 说:

8.3.5 更改:在 C++ 中,使用空参数列表声明的函数不带任何参数。 在C中,一个空的
参数列表意味着函数参数的数量和类型未知。

示例:

int f(); 
  // 在C++中表示int f(void) 
  // C 中的 int f( 未知 ) 
  

理由:这是为了避免错误的函数调用(即,函数调用的数量或类型错误)
论据)。

对原始功能的影响:更改明确定义的功能的语义。 此功能在 C 中被标记为“过时”。

8.5.3 函数 说:

4. 参数声明子句确定可以指定的参数及其处理,当
该函数被调用。 [...] 如果参数声明子句为空,则该函数
不接受任何争论。 参数列表(void)相当于空参数列表。

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.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://stackoverflow.com/a/36292431/895245

安人多梦 2024-07-11 03:31:16

我知道你的问题与 C++ 有关,但是当涉及到 C 时,答案可以在 K&R< 中找到/a>,第 72-73 页:

此外,如果函数声明不包含参数,如下所示

double atof(); 
  

这也被认为意味着没有任何关于
atof 的论据; 所有参数检查均已关闭。 这个特别的
空参数列表的含义是为了允许旧的 C
使用新编译器编译的程序。 但使用它是一个坏主意
与新节目。 如果函数接受参数,则声明它们; 如果
它不需要参数,使用 void。

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.

夜空下最亮的亮点 2024-07-11 03:31:16

历史记录:此答案适用于 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 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++).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文