I cannot figure this out:
int main() {
int (*) (int *) = 5;
return 0;
}
The above assignment compiles with g++ c++11. I know that int (*) (int *)
is a pointer to a function that accepts an (int *)
as argument and returns an int, but I do not understand how you could equate it to 5. At first I thought it is a function that constantly returns 5 (from my recent learning in F#, probably, haha), then I thought, briefly, that the function pointer points to memory location 5, but that does not work, clearly, and neither does hex values.
Thinking that it could be because the function returns an int, and that assigning an int is ok (somehow), I also tried this:
int * (*) (int *) = my_ptr
where my_ptr
is of type int *
, the same type as this second function pointer, as in the first case with type int. This does not compile. Assigning 5, or any int value, instead of my_ptr
, doesn't compile for this function pointer either.
So what does the assignment mean?
Update 1
We have confirmation that it is a bug, as shown in the best answer. However, it is still not known what actually happens to the value that you assign to the function pointer, or what happens with the assignment. Any (good) explanations on that would be very much appreciated! Please refer to the edits below for more clarity on the problem.
Edit 1
I am using gcc version 4.8.2 (in Ubuntu 4.8.2)
Edit 2
Actually, equating it to anything works on my compiler. Even equating it to a std::string variable, or a function name that returns a double, works.
Edit 2.1
Interestingly, making it a function pointer to any function that returns a data type that is not a pointer, will let it compile, such as
std::string (*) () = 5.6;
But as soon as the function pointer is to a function that returns some pointer, it does not compile, such as with
some_data_type ** (*) () = any_value;
error: expected identifier or '(' before ')' token
– tivnint *x = 5
you named itx
. Withint * (*x) (int *) = 5
it will not compile. (albeit that will compile as C code). – nosint(*) = 5;
andint(*);
– Johannes Schaub - litb