为什么C和C ++之间的未命名参数警告discrepency? [英] Why the unnamed parameter warning discrepency between C and C++?

查看:119
本文介绍了为什么C和C ++之间的未命名参数警告discrepency?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的C / C ++程序,和我一起上的所有警告编译。

I have the following C/C++ program, and I compile it with all warnings on.

INT美孚(INT X){返回5; }

C ++编译器给了我一个关于未引用的形式参数的警告。当我删除X,这样的签名写着INT美孚(INT),编译器是幸福的。

The C++ compiler gives me a warning about unreferenced formal parameter. When I remove "x" so that the signature reads "int foo(int)", the compiler is happy.

C编译器,在另一方面,喜欢命名参数和发出警告当它命名。

The C compiler, on the other hand, likes the named parameter and issues warning when it is unnamed.

修改:它会发出一个错误,而不是警告

EDIT: It issues an error, not warning.

为什么会有差别?什么道理呢?

Why the difference? What's the rationale?

P.S。我使用GNU编译器工具链。

P.S. I'm using the GNU compiler toolchain.

推荐答案

对于C的一个原因可能是,它仍然有旧式参数列表仅由标识符:

One reason for C might be that it still has the old-style parameter list that only consist of identifiers:

int f(to)
double to; {
  return to;
}

因此​​,基本上,当它仅看到一个标识符,它必须假设这是一个变量名而不是一个类型。

So basically when it only sees an identifier, it has to assume that this is a variable name and not a type.

这篇关于为什么C和C ++之间的未命名参数警告discrepency?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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