Function call missing argument list warning

后端 未结 2 1417
猫巷女王i
猫巷女王i 2021-01-18 12:57

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

相关标签:
2条回答
  • 2021-01-18 13:20
    1. There is no "call to accept". See #3.

    2. Because of #1.

    3. 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`.
      

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

      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.

    4. 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.

    0 讨论(0)
  • 2021-01-18 13:31

    In C++, any expression followed by a semicolon is a legal statement. (Why? Because C let you do this, I think). That means that all of the following are legal statements:

     5;
     3 + 5;
     1 % 2 == 0;
    

    The effect of a statement of this form is that the expression is evaluated and then discarded. A good optimizing compiler would just eliminate all of the logic here, since none of these have any side-effects.

    In your case, writing

    accept;
    

    is a legal statement because accept is an expression evaluating to a reference to the function accept. That means that accept; as a statement means "evaluate the address of accept, then discard it." The reason that no function is called here is that a function name by itself doesn't invoke the function; you need the parentheses (the function call operator) to actually make the call. This is useful, for example, if you want to pass a function into another function. For example, you might want to pass a comparison function into std::sort, like this:

    std::sort(range.begin(), range.end(), nameOfMyComparisonFunction)
    

    Here, it would be a real problem if this tried calling nameOfMyComparisonFunction, since the arguments can't be known until the sorting routine starts off.

    So why is this a warning and not an error? Well, it's perfectly legal C++ code, so the compiler can't compliantly call it an error. However, the compiler is right to flag it as a warning, since it almost certainly means you made an error. That said, most compilers have some setting that reports warnings as errors, and if you crank up the warning level high enough the compiler probably would say "this is so suspicious that I'm going to assume you messed something up."

    As to your last one - why does

    bool a = accept;
    

    end up setting a to true? In C++, any non-null pointer implicitly converts to true and any null pointer implicitly converts to false. In C++, functions are implicitly convertible to pointers to themselves, so in this case accept evaluates to the address of the accept function, which is non-null, so it sets a to true. When you then write

    cout << a << endl;
    

    the value is printed as 1, because bool values are printed as 1 and 0 rather than true and false by default. That said, you could write

    cout << boolalpha << a << endl;
    

    and you'll see true printed instead.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题