`f(void)` meaning no parameters in C++11 or C?

后端 未结 5 771
忘掉有多难
忘掉有多难 2021-01-07 17:58

In C++11 the following function declaration:

int f(void);

means the same as:

int f();

A pa

5条回答
  •  隐瞒了意图╮
    2021-01-07 18:01

    From "A History of C++" (1979− 1991 by Bjarne Stroustrup) at p11:

    C with Classes introduced the notation f(void) for a function f that takes no arguments as a contrast to f() that in C declares a function that can take any number of arguments of any type without any type check.

    It says later, however, that soon the empty function declarator was given its obvious meaning and the new construct - kinda rendered obsolete for the time. I guess nobody has bothered removing it after (or maybe there had been already some C++ code written with it that have needed support).

    In C:

    This construct however rendered important role as of the standardization of the C language where function prototyping was borrowed directly from C++. In this case f(void) was useful in supporting existing C code (in which the notion f() was already reserved as to indicate a function taking unspecified number of arguments).

    Before that the C language hadn't been able to attach specific types to each parameter but only functions with unspecified number of arguments and with unspecified types were declarable using the f() form.

提交回复
热议问题