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

前端 未结 4 513
情话喂你
情话喂你 2020-11-22 16:00

Consider these two function definitions:

void foo() { }

void foo(void) { }

Is there any difference between these two? If not, why is the <

4条回答
  •  一整个雨季
    2020-11-22 16:30

    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.

提交回复
热议问题