C ++或C中foo(void)和foo()之间有区别吗?

C ++或C中foo(void)和foo()之间有区别吗?

Is there a difference between foo(void) and foo() in C++ or C?

考虑这两个函数定义:

1
2
3
void foo() { }

void foo(void) { }

这两者有什么区别吗? 如果没有,那为什么void参数呢? 美学原因?


在C:

  • void foo()表示"函数foo采用未指定数量的未指定类型的参数"
  • void foo(void)表示"函数foo不带参数"

在C ++中:

  • void foo()表示"函数foo不带参数"
  • void foo(void)表示"函数foo不带参数"

因此,通过编写foo(void),我们在两种语言中实现相同的解释,并使我们的标题多语言(尽管我们通常需要对标题做更多的事情,使它们真正成为跨语言;即,将它们包装在如果我们正在编译C ++)。


我意识到你的问题与C ++有关,但是当谈到C时,答案可以在K&R,第72-73页找到:

Furthermore, if a function declaration does not include arguments, as
in

1
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标准草案

没有区别。

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

附件C"兼容性"C.1.7第8条:申报人说:

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:

1
2
3
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功能说:

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

正如C ++ 11所提到的,int f()没有指定参数,而且过时了。

它可以导致工作代码或UB。

我在以下网址详细解释了C99标准:https://stackoverflow.com/a/36292431/895245


在C中,在空函数引用中使用void,以便编译器具有原型,并且该原型具有"无参数"。在C ++中,您不必告诉编译器您有原型,因为您不能省略原型。


推荐阅读

    linuxrm命令参数?

    linuxrm命令参数?,系统,命令,文件,目录,环境,档案,文件夹,终端,参数,子目录,l

    linux命令行定义参数?

    linux命令行定义参数?,系统,信息,名称,实时,命令,百分比,工作,周期,选项,参

    linux命令查询参数?

    linux命令查询参数?,网络,信息,设备,系统,服务,状态,情况,工作,地址,命令,Lin

    linux网络参数命令?

    linux网络参数命令?,网络,地址,系统,工作,信息,工具,情况,服务,状态,命令,Lin

    linux的cd命令参数?

    linux的cd命令参数?,工作,地址,系统,命令,一致,目录,信息,基础,名称,管理,lin

    linux下cd命令参数?

    linux下cd命令参数?,工作,命令,系统,一致,名称,目录,用户,缩写,意思,参数,lin

    linux解压命令的区别?

    linux解压命令的区别?,系统,工具,电脑,命令,文件,资料,平台,档案,名称,文件

    linux各种命令的参数?

    linux各种命令的参数?,网络,信息,工作,地址,系统,情况,服务,命令,软件,数据,l

    linux命令行参数长度?

    linux命令行参数长度?,系统,信息,实时,工具,工作,名称,环境,百分比,标准,情

    linux添加命令参数?

    linux添加命令参数?,系统,管理,工作,基础,命令,情况,网络,工具,代码,环境,别

    linux命令和程序区别?

    linux命令和程序区别?,系统,标准,环境,情况,控制权,服务,软件,信息,命令,文

    linux命令中和区别?

    linux命令中和区别?,系统,认证,电脑,网址,标准,工作,工具,位置,命令,文件,了

    linux中和命令的区别?

    linux中和命令的区别?,系统,网络,软件,标准,命令,控制权,基础,平台,代码,投

    linux命令参数长度?

    linux命令参数长度?,工作,系统,信息,基础,网络,环境,情况,管理,长度,命令,Lin

    linux安装参数命令?

    linux安装参数命令?,工作,系统,软件,地址,命令,中心,在线,地方,位置,信息,Lin

    linux的命令的参数?

    linux的命令的参数?,网络,工作,系统,信息,情况,服务,命令,状态,数据,标准,Lin

    linux命令传入参数?

    linux命令传入参数?,系统,地址,工作,命令,信息,名称,代码,名字,服务,管理,如

    linux命令行参数的和?

    linux命令行参数的和?,系统,地址,管理,命令,基础,数字,网络,情况,下降,信息,

    linux关机命令区别?

    linux关机命令区别?,系统,工作,命令,信息,用户,进程,方式,方法,终端,区别,lin

    linux带参数的命令?

    linux带参数的命令?,网络,地址,工作,信息,系统,情况,状态,服务,目录,命令,Lin