Using std::bind with member function, use object pointer or not for this argument?

前端 未结 3 1878
走了就别回头了
走了就别回头了 2020-11-28 05:31

When using std::bind to bind a member function, the first argument is the objects this pointer. However it works passing the object both as a point

3条回答
  •  失恋的感觉
    2020-11-28 05:36

    Both are correct. 20.8.9.1.2 forwards to 20.8.2 to describe the requirements and the effect of your call to bind. 20.8.2 is:

    20.8.2 Requirements [func.require]

    1 Define INVOKE(f, t1, t2, ..., tN) as follows:

    (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T;

    ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is not one of the types described in the previous item;

    t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T;

    (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1 is not one of the types described in the previous item;

    f(t1, t2, ..., tN) in all other cases.

    The first two options allow both a reference and a pointer.

    The important thing to notice here is that the wording does not limit you to plain pointers. You could use a std::shared_ptr or some other smart pointer to keep your instance alive while bound and it would still work with std::bind as t1 is dereferenced, no matter what it is (given, of course, that it's possible).

提交回复
热议问题