无效的,无效的,C和C ++ [英] void, VOID, C and C++

查看:142
本文介绍了无效的,无效的,C和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

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

(使用Fedora 10的GCC 4.3.2),它编译使用C就好了。编译为C ++一样code使我有以下错误:

which compiles just fine in C (using gcc 4.3.2 on Fedora 10). The same code compiled as C++ gives me the following error:

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

现在,这个东西在外部库,我想主人来解决这个问题。所以,我有一个问题 - 没有C ++标准禁止了这样构造?你能不能给我一个指针/引文?我记得的唯一一件事是,函数声明与(无效)信号空参数列表是C ++ pcated德$ P $,但我不明白为什么通过typedef 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? 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 ++无效C89和,但在C99是有效的。

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

从C99草案,TC2( 6.7.5.3/10

From The C99 draft, TC2 (6.7.5.3/10):

void类型的未命名参数的特殊情况下,如在列表中的唯一项目
  特定网络上课,该函数没有参数。

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.

从C ++标准, 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.

这意味着以作废,而不是一般的类型无效的,也可以从哪里模板参数推导失败案例之一(看到的实际关键字14.8.2 / 2

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。

  •   

这是把别人明确,明显的这里 >和其他的答案与一些GCC错误报告。

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


要回顾一下,你的GCC是正确的,但早期版本的GCC是错误的。因此,该code可能已经成功地用它编译早。用户应该修正code,以便它使用作废两种功能,那么它也编译其它编译器(科莫还拒绝与第二个声明VOID)。

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

这篇关于无效的,无效的,C和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆