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

前端 未结 4 531
情话喂你
情话喂你 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

    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++).

提交回复
热议问题