void, VOID, C and C++

后端 未结 3 1882
清酒与你
清酒与你 2020-12-02 00:15

I have the following code:

typedef void VOID;
int f(void);
int g(VOID);

which compiles just fine in C (using gcc 4.3.2 on Fedora 10). The s

相关标签:
3条回答
  • 2020-12-02 00:29

    gcc bugs. Edit: since it wasn't clear enough, what I meant was gcc 4.3.2 was compiling it due to bugs. See #32364 and #9278.

    0 讨论(0)
  • 2020-12-02 00:29

    I just put your code in a .cpp file, and it compiled with no problems in VS2005, SUSE, Redhat, and Solaris, so I guess your specific gcc version does not approve of this. Gal

    0 讨论(0)
  • 2020-12-02 00:34

    Yes, as far as i know the second declaration is invalid in C++ and C89, but it is valid in C99.

    From The C99 draft, TC2 (6.7.5.3/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.

    It's explicitly talking about the type "void", not the keyword.

    From The C++ Standard, 8.3.5/2:

    If the parameter-declaration-clause is empty, the function takes no arguments. The parameter list (void) is equivalent to the empty parameter list.

    That it means the actual keyword with "void", and not the general type "void" can also be seen from one of the cases where template argument deduction fails (14.8.2/2):

    • Attempting to create a function type in which a parameter has a type of void.

    It's put clear by others, notable in one core language issue report here and some GCC bugreports linked to by other answers.


    To recap, your GCC is right but earlier GCC versions were wrong. Thus that code might have been successfully compiled with it earlier. You should fix your code, so that it uses "void" for both functions, then it will compile also with other compilers (comeau also rejects the second declaration with that "VOID").

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