dimanche 8 mai 2022

Why there is no implict conversions to pointers of member functions

When used std::bind, I found & is necesssary to get the address of member functions, while isn't necessary for regular functions, for example:

class Obj{
public:
    void member_func(int val);
};

void func(int val);

int main(){
    Obj obj;
    auto mf1 = std::bind(&Obj::member_func, &obj, 1); // ok
    auto mf2 = std::bind(Obj::member_func, &obj, 1); // error
    
    auto f1 = std::bind(&func, 1); // ok
    auto f2 = std::bind(func, 1); // ok
}

And after STFW(Search The Friendly Web :) ), I found this in cpp reference

An lvalue of function type T can be implicitly converted to a prvalue pointer to that function. This does not apply to non-static member functions because lvalues that refer to non-static member functions do not exist.

Which explains why & is necessary for member functions, but I still don't fully understand why there is no implict conversions to pointers of member functions?

In particular, what does "lvalues that refer to non-static member functions do not exist" mean?

First time to ask question, hope I clearly explained my question.

Aucun commentaire:

Enregistrer un commentaire