foo == (* foo)

This is a question of theory.

Functions are pointers and, as the following code shows, the dereferenced value of a function pointer is itself.

1
2
3
4
5
6
7
8
9
10
11

#include <iostream>

void foo(){};
    
int main(){

auto f = foo;

std::cout << (f == (* f) ) << std::endl;
}


This program prints the value 1

What is the right way to conceptualize that? For example, should one think of function pointers as pointers that point to themselves? Or is it rather that a dereferenced function pointer is cast into a pointer? Or something else

Last edited on
What is the right way to conceptualize that?

Function types undergo the function-to-pointer conversion.

We have static_assert(std::is_same<void(&)(), decltype(*f)>::value).

As that makes clear, the expression *f is certainly not a pointer (although f is a pointer).
When a pointer-to-function is dereferenced, the result is a function; when a function is dereferenced, it undergoes the function-to-pointer conversion.

The statement "there are no values of function type" is quite similar to the claim "there are no values of array type".
Functions, like arrays, lack value semantics.

http://coliru.stacked-crooked.com/a/aa4872eddca58a5a
Last edited on
Very, clear! Thank you very much.
Topic archived. No new replies allowed.