vendredi 25 septembre 2015

std::bind using placeholder for this argument of member function

Consider the following code:

struct Foo {
  int func(int bar) {
    return bar + 1;
  }
};

int main() {
  auto func_ref = std::bind(&Foo::func, std::placeholders::_1, 4);
  Foo foo;
  std::cout << func_ref(&foo);
}

This code does not compile. It seems std::bind does not allow leaving the "this" argument of the member function as a placeholder. Is there any way I can do this without just using a lambda, i.e.

auto func_ref = [](Foo* foo) {return foo->func(4);};

Aucun commentaire:

Enregistrer un commentaire