Visual Studio 的 C4028 警告(形式参数与声明不同)是虚假的吗? [英] Is Visual Studio's C4028 warning (formal parameter different from declaration) spurious?

查看:185
本文介绍了Visual Studio 的 C4028 警告(形式参数与声明不同)是虚假的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下函数声明和定义.在头文件中:

Consider the following function declaration and definition. In the header file:

void some_function(int param);

在源文件中:

#include "test.h"

void some_function(const int param) {}

int main(void) {
    return 0;
}

在Visual Studio 2010下,编译为纯C项目,我看到warning C4028:formal parameter 1 different from declaration.但据我所知,这是完全有效的 C,而且是相当普遍的做法.

Under Visual Studio 2010, compiling as a pure C project, I see warning C4028: formal parameter 1 different from declaration. But as far as I know, this is perfectly valid C, and fairly common practice.

我对此是否错了,因此 VS2010 是否正确警告我?或者,如果这是一个虚假警告,我是否可以专门针对这种情况禁用它,但在参数类型不匹配的实际情况下保持警告?

Am I wrong about this, and is VS2010 therefore correct to warn me? Or if it is a spurious warning, can I disable it specifically for this kind of case, but keep the warning on for actual cases of mismatched parameter types?

(VS2010 自带的实际编译器是:Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86.我的命令行只是cl test.c.)

(The actual compiler that comes with VS2010 is: Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86. My command line is simply cl test.c.)

推荐答案

C89 3.5.4.3 Function declarators 关于参数是这样说的:

C89 3.5.4.3 Function declarators has this to say about the parameters:

要使两种函数类型兼容,都应指定兼容的返回类型.此外,参数类型列表(如果两者都存在)应在参数数量和省略号终止符的使用方面达成一致;对应的参数应该有兼容的类型.

For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types.

3.1.2.6 兼容类型和复合类型部分将兼容类型指定为具有相同类型的类型.它引用了 3.5.3 的类型限定符(其中 const 是其中之一)并且 that 指出:

Section 3.1.2.6 Compatible type and composite type specifies compatible types as those having the same type. It references 3.5.3 for type qualifiers (of which const is one) and that states:

要使两个限定类型兼容,两者都应具有兼容类型的相同限定版本;说明符或限定符列表中类型限定符的顺序不影响指定的类型.

For two qualified types to be compatible, both shall have the identically qualified version of a compatible type; the order of type qualifiers within a list of specifiers or qualifiers does not affect the specified type.

现在你通常会认为 intconst int兼容的类型.但是,我认为这是对标准的误读,因为 int 不是限定类型,因此上面的引用不适用.

Now you would normally consider therefore that int and const int are not compatible types. However, I believe that's a mis-reading of the standard simply because int is not a qualified type, therefore that quote above doesn't apply.

3.5.4.3 后面有一个更适用的引用,说明:

There is a more applicable quote later on in 3.5.4.3 stating:

对于每个用限定类型声明的参数,这些比较的类型是其声明类型的非限定版本.

For each parameter declared with qualified type, its type for these comparisons is the unqualified version of its declared type.

因此 intconst int 比较类似,应该被允许.但是这只是意味着根据标准这不是错误.可能是微软,以他们的智慧,仍然认为这是一个可疑的行为,也是发出警告的一个很好的理由.

Hence int and const int are compared similarly and should be allowed. But that just means it's not an error according to the standard. It may be that Microsoft, in their wisdom, still think it's a suspect action and a good reason to pump out a warning.

即使您认为这是一个问题,Microsoft 也不太可能为您修复它(见下文).

Even if you consider that a problem, it's very unlikely Microsoft are going to fix it for you (see below).

现在您可能想知道为什么在有更多现代标准可用时我要引用 C89.

Now you may wonder why I'm quoting C89 when there are far more modern standards available.

这是因为 Microsoft 毫不掩饰 Visual C++ 主要是一个 C++ 编译器,并且只能将 C 代码编译为标准的早期迭代,根据 此处:

It's because Microsoft makes no secret that Visual C++ is primarily a C++ compiler and can compile C code only to the earlier itertions of the standard, as per here:

感谢您抽出宝贵时间向我们发送您的建议.目前,没有计划在 VS2010 中实现 C99 支持.一旦我们完成了这个产品周期,我们将审查所有客户的建议,包括这一建议,以供我们未来的规划.- 马克·罗伯茨,微软.

Thanks for taking the time to send us your suggestion. Currently, there are no plans to implement C99 support in VS2010. Once we complete this product cycle, we will be reviewing all customer suggestions, including this one, for our future planning. - Mark Roberts, Microsoft.

而且,来自 维基百科页面:

根据 Herb Sutter 的说法,C 编译器只是出于历史原因"而被包含在内,并没有计划进一步开发.建议用户要么只使用 C 语言的子集也是有效的 C++,然后使用 C++ 编译器编译他们的代码,或者只使用不同的编译器,如英特尔 C++ 编译器或 GNU 编译器集合.

According to Herb Sutter, the C compiler is only included for "historical reasons" and is not planned to be further developed. Users are advised to either use only the subset of the C language that is also valid C++, and then use the C++ compiler to compile their code, or to just use a different compiler such as Intel C++ Compiler or the GNU Compiler Collection instead.

这在 Herb Sutter 自己的博客中得到了证实,相关文章 此处:

This is confirmed in Herb Sutter's own blog, the relevant article here:

我们不打算支持不属于 C90 或 ISO C++ 的 ISO C 功能.

We do not plan to support ISO C features that are not part of either C90 or ISO C++.

这篇关于Visual Studio 的 C4028 警告(形式参数与声明不同)是虚假的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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