Function call missing argument list warning

后端 未结 2 1416
猫巷女王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.

提交回复
热议问题