Passing member function with all arguments to std::function

前端 未结 3 993
醉梦人生
醉梦人生 2020-12-24 15:52

How can I create a std::function from member function without need for typing std::placeholders::_1, std::placeholders::_2, etc - I would like to \"placehold\" all arguments

3条回答
  •  别那么骄傲
    2020-12-24 16:24

    You can easily do this with variadic generic lambdas in C++14:

    template
    auto smart_bind(F f, C* c)
    {
      return [c, f](auto&&... args) { return (c->*f)(std::forward(args)...); };
    }
    
    // In your code:
    std::function fun2 = smart_bind(&Foo::bar, &object);
    

    Live demo: https://ideone.com/deR4fy

提交回复
热议问题