Why can't a function pointer be bound to an lvalue reference, but a function can?

What is the difference between using a function name with an ampersand and without it? I noticed that they behave differently when associated with a template parameter:

void foo();
template <typename F> void bind(F&);
bind(foo);  // OK
bind(&foo); // Error

Why is that? What is an inferred type in these cases?

+4
source share
2 answers

, & prvalue T*. , &foo void (*)(), prvalue lvalue -const; , bind() , .

bind(foo) pamareter F (.. void()), bind void(&)(), .

, foo &foo - --. , bind() . bind(foo) -- , . , bind(&foo), operator& , printerue , bind().

, --, bind(foo) bind(&foo) , .

void foo();
template <typename F> void bind(F);
bind(foo);  // OK, F=void(*)()
bind(&foo); // same as above
+4

&foo , const.

template <typename F> void bind(const F&);

.


foo , .

+2

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


All Articles