为什么C ++代码在函数定义中缺少一个正式的参数名称而没有警告? [英] Why does C++ code missing a formal argument name in a function definition compile without warnings?

查看:276
本文介绍了为什么C ++代码在函数定义中缺少一个正式的参数名称而没有警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始使用一些VS2005生成的MFC代码时,我注意到它覆盖了一个类似这样的方法:

  void OnDraw(CDC * / * pDC * /)
{
...
// TODO:在此处添加您的代码
}



当然,一旦我添加了一些东西,我意识到我需要取消注释pDC形式参数为了编译,但我很困惑如何/为什么C ++函数可以编译(没有警告),当形式参数只有一个类型而不是一个名称:

  void foo(int)
{
int x = 3;
}
int main()
{
foo(5);
return 0;
}

不应该生成至少一个警告(带-Wall或/ W4 )?它似乎不。我缺少什么?是否有一种情况,这是有用的,还是只是因为编译器不能告诉一个函数声明(只需要类型)和一个定义(完全指定)之间的差异,直到该行被处理后?

解决方案

因为有时你有一个接口需要的参数,但是函数不使用它。也许参数不再是必要的,只有在必须使用相同签名的其他函数中才是必需的(尤其是它们可以通过指针调用)或者功能尚未实现。没有使用的参数在生成或框架代码中可能是特别常见的(这可能是为什么MFC生成的代码已注释掉名称)。



至于为什么没有警告 - 我想这是因为这是一个问题是一个主观的事情,其他人(特别是编译器实现者)不认为它是一个问题。一旦你实际使用参数,你会得到编译器抱怨,如果你忘记取消注释的名称,所以你得到的编译器抱怨只有当你真正需要它(编译器的版本的敏捷YAGNI:你竞技场



相反的情况似乎通常发生在你启动警告 - 命名参数没有使用时产生警告 - 这可能是为什么生成的函数名称已注释掉。


While getting started with some VS2005-generated MFC code, I noticed it overrode a method with something like this:

void OnDraw(CDC* /*pDC*/)
{
    ...
    // TODO: Add your code here
}

So of course, as soon as I added something I realized I needed to un-comment the pDC formal argument in order to compile, but I'm confused as to how/why a C++ function can compile (with no warnings) when the formal argument only has a type and not a name:

void foo(int)
{
    int x = 3;
}
int main()
{
    foo(5);
    return 0;
}

Shouldn't this generate at least a warning (with -Wall or /W4)? It doesn't seem to. Am I missing something? Is there a case where this is useful or is it just because the compiler can't tell the difference between a function declaration (only types required) and a definition (fully specified) until after the line has been processed?

解决方案

Because sometimes you have a parameter that's required by an interface but the function doesn't use it. Maybe the parameter is no longer necessary, is only necessary in other functions that must use the same signature (especially so they can be called through pointers) or the functionality hasn't been implemented yet. Having parameters that aren't used can be particularly common in generated or framework code for this reason (and that's probably why the MFC generated code has the name commented out).

As to why there's no warning - I guess it's because whether this is a problem is a subjective thing and other people (particularly compiler implementers) don't see it as a problem. Once you actually go to use the parameter, you'll get the compiler to complain if you forget to uncomment the name so you get the compiler complaining only when you really need it to (the compiler's version of the agile YAGNI: "You Aren’t Gonna Neet It" philosophy).

The opposite does seem to generally occur when you crank up warnings - named parameters that aren't used generate warnings - again that's probably why the generated function has the name commented out.

这篇关于为什么C ++代码在函数定义中缺少一个正式的参数名称而没有警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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