简体   繁体   中英

What is the meaning of this statement in the C++11 Standard?

What do the characters in bold mean in this sentence extracted from paragraph §5.2.2/1 of the C++11 Standard?

There are two kinds of function call: ordinary function call and member function (9.3) call. A function call is a postfix expression followed by parentheses containing a possibly empty, comma-separated list of expressions which constitute the arguments to the function. For an ordinary function call, the postfix expression shall be either an lvalue that refers to a function (in which case the function-to-pointer standard conversion (4.3) is suppressed on the postfix expression), or it shall have pointer to function type .

Edit

Basically what I'm asking is: when the Standard says "(in which case the function-to-pointer standard conversion is suppressed on the postfix expression)" does it mean that this suppression is for good or that it will be revoked later (eg after function overloading)?

Edit1

In my opinion the word "suppressed" above is misleading since it gives the impression that the conversion from function name to a function pointer might never be done by the compiler. I believe that's not the case. This conversion will always occur after the function overloading process is finished, since only at this point, the compiler knows exactly which function to call. That's not the case when the program initializes a function pointer. In this situation, the compiler knows the function to be called, so there will be no overloading, as the example below shows.

#include <iostream>
int foo(int i) { std::cout << "int" << '\n'; return i * i; }
double foo(double d) { std::cout << "double" << '\n'; return d * d; }

int main()
{
    int(*pf)(int) = foo;                //  no overloading here!
    std::cout << pf(10.) << '\n';       //  calls foo(int)
    std::cout << foo(10.) << '\n';      //  call foo(double) after function overloading. Therefore, only after function
                                        //  overloading is finished, the function name foo() is converted into a function-pointer.
}

The code prints:

int
100
double
100

For an ordinary function call, the postfix expression shall be either an lvalue that refers to a function (in which case the function-to-pointer standard conversion (4.3) is suppressed on the postfix expression), or it shall have pointer to function type.

So, "postfix expression" is defined in 5.2/1... it's a grammatical element that doesn't necessary mean the " x++ " kind of postfix... have a look at the list itself. Basically, the point is that there's an expression that:

  • "shall be either an lvalue that refers to a function (in which case the function-to-pointer standard conversion (4.3) is suppressed on the postfix expression)"

This means the expression is actually the identifier for a function, or something like (x ? fn1 : fn2) . It doesn't need to undergo conversion to a pointer - the compiler will already know how to call a function.

(The reason it's noteworthy that it must not undergo conversion is Operator Precedence - specifically suffixed ++ and -- , () for function calls, [] , . , and -> all have identical precedence and left-to-right associativity, so eg [1]f(1,2,3) is treated as [1](f(1,2,3)) because it's the only parsing match, whereas if decay to a pointer took place then the left-to-right associativity would treat it as ([1]f)(1,2,3) .)

  • " or it shall have pointer to function type."

So, you can also provide a function pointer.

Absent any statement in the Standard to the contrary, the suppression is presumably permanent. I think the conversion is suppressed because (i) overload resolution needs to take place and (ii) it is not necessary in this situation. Indeed, it would be impossible to convert to a pointer before we knew exactly which function is being called. After overload resolution, we know we are directly calling the function, so a conversion would be pointless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM