2

I know with function pointers, you can call the function equivalently either using

 (*fptr)();
 //or 
 fptr(); 

But how do compilers achieve this? Do they completely disregard the reference token, or is there something more involved here?

Consider this valid c++ code:

void f1() { std::cout << "hi\n"; }
int main() {
    auto f = f1;
    (********************f1)();
    (*(*(*f1)))();
    ***************f1;
}

Are all the deference operators simply ignored?

And why is it not symmetrical with taking the address of functions? Here you may refer to a function with & or without, but you cannot use multiple & signs.

For example:

auto pf1 = f1; // ok
auto pf2 = &f1; // ok 
//  auto pf3 = &(&(f1)); // does not compile
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Gonen I
  • 5,576
  • 1
  • 29
  • 60
  • 1
    Possible duplicate of [Function pointer vs Function reference](https://stackoverflow.com/questions/19200513/function-pointer-vs-function-reference). – François Andrieux May 11 '21 at 13:48
  • Think about unary + operator on int: `+ + + + + + +42`. we just apply the operator (which indeed decays to pointer for function reference). – Jarod42 May 11 '21 at 13:58

0 Answers0