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

后端 未结 5 772
忘掉有多难
忘掉有多难 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

    In C++ both are the same thing.

    In C, f() means that we don't know how many parameters the function takes at this point. It is unspecified parameters. And f(void) means that this function does not take any parameters.

    From the C standard :

    6.7.6.3 Function declarators (including prototypes)

    6/ A parameter type list specifies the types of, and may declare identifiers for, the parameters of the function.

    10/ The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

    14/ An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.

    And like you said, in the C++ standard :

    8.3.5 Functions [dcl.fct]

    4/ The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [ Note: *the parameter-declaration-clause* is used to convert the arguments specified on the function call; see 5.2.2. —end note ] If the parameter-declaration-clause is empty, the function takes no arguments. A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-07 18:06

    In C++ they both mean the same thing.

    In C f(void) is different from f(), becuse f() means "unspecified parameters" - you can legally pass anything (whether the function at receiving the data is happy about that or not is another matter).

    0 讨论(0)
  • 2021-01-07 18:11

    This comes to C++ from C. In C f() means unknown number and type of parameters. So in C for no parameters there is f( void ).

    In C++ it's redundant: f() and f( void ) mean same thing - a function that has no parameters.

    0 讨论(0)
  • In C++, there is no difference. However, this is inherited from C, where int f() mean "function which can take any number of arguments of any types" and int f(void); specifies functions that takes no arguments.

    Edit As Angew pointed out, in C, f() means "function whose parameters are unknown at this point." It does not mean it can take any number of arguments - close to that would be f(T arg, ...), where arg is at least one named parameter before ..., which accepts at least one argument, arg (as pointed by @hvd).

    0 讨论(0)
提交回复
热议问题