函数调用缺少参数列表警告 [英] Function call missing argument list warning

查看:107
本文介绍了函数调用缺少参数列表警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读"C ++编程语言-第四版",我正在键入一个简单的练习,目的只是为了掌握C ++语法,而我偶然偶然发现了让我大吃一惊的东西.简而言之,我忘记在main的 accept 上添加():

I'm reading over "The C++ Programming Language - Fourth Edition" and I was typing up a simple exercise just to get a hang of C++ syntax and I accidentally stumbled across something that made me raise an eyebrow. In short, I forgot to add () on accept in main:

bool accept()
{
    cout << "Do you want to proceed (y or n)?\n";

    char answer = 0;
    cin >> answer;

    if (answer == 'y')
    {
        return true;
    }
    return false;

}

int main()
{
    accept;
}

这会运行并编译并生成(在VS2015中)a

This runs and compiles and produces (in VS2015) a

C4551-函数调用缺少参数列表

C4551 - function call missing argument list

我发现自己正在阅读 lambdas 和一堆应该关闭关于SO的问题,因为它们大多要求调试我的代码".

I've found myself reading lambdas and a bunch of questions on SO that should be be closed because they are mostly asking to "debug my code."

我认为,如果代码可以编译并运行,并且该函数包含阻塞语句(等待用户输入)和返回类型,则所有代码都将按预期执行,而不会遗漏括号.事实并非如此.

I figured that if the code compiles and runs, and the function contains a blocking statement (waiting for user input) and a return type, that all the code would get executed as expected regardless of missing the parentheses; that is not the case though.

另外,我认为我将把main中的 accept 的调用更改为 bool a = accept;.cout<<a; 尝试阻止任何优化(如果实际上是在发生这种情况),并且也未调用 accept()代码.

Additionally, I figured I would change the call to accept in main, to bool a = accept; cout << a; to try and prevent any optimization (if that was what was actually happening), and that did not call the accept() code either.

我很好奇的是:

  1. accept 进行编译的调用是什么?
  2. 为什么 accept 中的代码没有被调用
  3. 为什么这只是一个警告,而不是一个错误(我知道我可以更改配置以将其显示为错误,所以我更加怀疑当实际结果与预期结果不同时,默认情况下如何接受此语法,因此"?"这个问题可能是基于观点的,如果您同意,则可以忽略.)
  4. 运行 bool a = accept;cout<<a; 中的main代码产生 1 作为输出.当 false 是默认的bool值(至少在C#中)并且由于没有执行接受代码而没有什么可返回true值时,怎么办?
  1. What is the call to accept getting compiled into?
  2. Why isn't the code in accept getting called
  3. Why is this only a warning, and not an error (I know I can change configuration to display it as error, I'm more so questioning how this is accepted syntax by default when actual result differs from expected result so "dramatically?" This question might be opinion-based, omit it if you agree it is.)
  4. Running the bool a = accept; cout << a; code in main produces 1 as the output. How can this be when false is the default bool value (in C# at least) and there is nothing to return a true value since the accept code does not get executed?

推荐答案

  1. 没有调用 accept ".参见#3.

由于#1.

使用不带函数名 的函数调用语法(即())意味着您正在访问函数本身.例如,您可以将其存储在函数指针中(通过衰减函数到指针):

The use of a function name without function call syntax (ie: ()) means that you're accessing the function itself. You could, for example, store it in a function pointer (through function-to-pointer decaying):

using my_func = bool(*)(); //Function that takes nothing and returns a bool.
my_func var = accept; //Store a pointer to `accept`.

然后您可以发出 var(); ,它会调用 accept .

You could then issue var();, which would call accept.

但是,由于您从不存储该函数,因此编译器猜测您可能打算调用该函数,而不是访问该函数的指针. accept; 是合法的C ++语句,因此编译器不能对此出错.它可能会发出警告,因为该语句无法完成任何操作,并且您可能打算调用该函数.它与 1; 之类的语句没有什么不同:完全合法,但完全没用.

However, because you never store the function, the compiler guesses that you probably meant to call the function, not to access the function's pointer. accept; however is a legal C++ statement, therefore the compiler cannot error on it. It can emit a warning, since the statement accomplishes nothing and you probably meant to call the function. It's no different from a statement like 1;: perfectly legal, but utterly useless.

之所以这样做是因为C ++的诡计.非空指针会衰减为布尔值 true .并且 accept 会衰减为不为null的函数指针.因此,当转换为 bool 时,它将为 true .您仍然没有调用该函数.

It does this because of C++ trickery. Non-null pointers decay to the boolean value true. And accept decays to a function pointer that is not null. Therefore, when converted to a bool, it will be true. You're still not calling the function.

这篇关于函数调用缺少参数列表警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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