简体   繁体   English

void,VOID,C和C ++

[英]void, VOID, C and C++

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). 它在C中编译得很好(在Fedora 10上使用gcc 4.3.2)。 The same code compiled as C++ gives me the following error: 编译为C ++的相同代码给出了以下错误:

void.c:3: error: ‘<anonymous>’ has incomplete type
void.c:3: error: invalid use of ‘VOID’

Now, this is something in external library and I would like the owner to fix that problem. 现在,这是外部库中的东西,我希望所有者解决这个问题。 So I have a question - does C++ standard forbids this construct? 所以我有一个问题 - C ++标准是否禁止这种结构? Could you give me a pointer/citation? 你能给我一个指针/引文吗? The only thing I can recall is that function declaration with (void) to signal empty parameter list is deprecated in C++, but I don't understand why typedefed VOID does not work. 我唯一记得的是在C ++中不推荐用函数声明(void)来表示空参数列表,但我不明白为什么typedefed VOID不起作用。

gcc bugs . gcc错误 Edit: since it wasn't clear enough, what I meant was gcc 4.3.2 was compiling it due to bugs. 编辑:因为它不够清楚,我的意思是gcc 4.3.2正在编译它由于错误。 See #32364 and #9278 . #32364#9278

Yes, as far as i know the second declaration is invalid in C++ and C89, but it is valid in C99. 是的,据我所知,第二个声明在C ++和C89中无效,但它在C99中有效。

From The C99 draft, TC2 ( 6.7.5.3/10 ): 来自C99草案,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. void类型的未命名参数作为列表中唯一项的特殊情况表明该函数没有参数。

It's explicitly talking about the type "void", not the keyword. 它明确地谈论类型“void”,而不是关键字。

From The C++ Standard, 8.3.5/2 : 来自C ++标准, 8.3.5/2

If the parameter-declaration-clause is empty, the function takes no arguments. 如果parameter-declaration-clause为空,则该函数不带参数。 The parameter list (void) is equivalent to the empty parameter list. 参数列表(void)等效于空参数列表。

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 ): 这意味着具有“void”的实际关键字,而不是通用类型“void”也可以从模板参数推断失败的情况( 14.8.2/2 )中看出:

  • Attempting to create a function type in which a parameter has a type of void. 试图创建一个函数类型,其中参数的类型为void。

It's put clear by others, notable in one core language issue report here and some GCC bugreports linked to by other answers. 其他人已经明确了这一点, 这里有一个核心语言问题报告还有一些与其他答案相关的GCC错误报告。


To recap, your GCC is right but earlier GCC versions were wrong. 总结一下,你的海湾合作委员会是正确的,但早期的GCC版本是错误的。 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"). 你应该修复你的代码,以便它对两个函数都使用“void”,然后它也将与其他编译器一起编译(comeau也拒绝使用那个“VOID”的第二个声明)。

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. 我只是把你的代码放在一个.cpp文件中,它在VS2005,SUSE,Redhat和Solaris中没有问题编译,所以我猜你的具体gcc版本不赞成这个。 Gal 加尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM