Is there no difference between a function address and a function address address?

void f() {} void test() { auto fn_1 = f; auto fn_2 = &f; assert(fn_1 == fn_2); // OK fn_1(); // OK fn_2(); // OK (*fn_1)(); // OK (*fn_2)(); // OK (**fn_1)(); // OK (**fn_2)(); // OK (***fn_1)(); // OK (***fn_2)(); // OK } 

Are these behaviors explicitly defined by the C ++ standard?

+4
source share
2 answers

Yes, ampersand is optional, they give the same result.

The value of the type of the function T can be converted to a prvalue of type "pointer to T". The result is a pointer to a function .55

I'm only going to go and say that if you are using C ++ 11, you should use std::function anyway, it is much easier to understand and use.

+1
source

The problem with the game here is that the function breaks up into a function pointer. The types of both variables fn_1 and fn_2 are void (*)() , that is, "a pointer to a function without arguments and return void ". In the case of fn_1 function f decays into a pointer to the function, while in the case of fn_2 you explicitly assign a pointer to the function fn_2 , and no decomposition occurs.

+1
source

Source: https://habr.com/ru/post/1502725/


All Articles