jeudi 29 décembre 2016

Always prefer [std::bind(&A::mem_fn, &obj)] to [std::bind(&A::mem_fn, obj)]?

#include <functional>

struct A
{
    A() = default;
    A(const A&) = delete;
    A& operator =(const A&) = delete;

    void foo() const
    {}
};

int main()
{
    A a;
    std::bind(&A::foo, &a); // ok
    std::bind(&A::foo, a);  // error

    return 0;
}

The example seems to say:

You should always prefer std::bind(&A::foo, &a); to std::bind(&A::foo, a);.

More serious, if copying the object of A is costly, the latter should be avoided.

I can't think out any case that the latter is better. So, I just wonder:

Why doesn't the C++ standard prohibit the latter?

Aucun commentaire:

Enregistrer un commentaire