在 std::bind 中省略 std::placeholders

omit std::placeholders in std::bind

要创建 std::function,我是这样做的:-

std::function<void(int,int,int)> f=
    std::bind(&B::fb,this,
        std::placeholders::_1,
        std::placeholders::_2,
        std::placeholders::_3
    );  

void B::fb(int x,int k,int j){} //example

显然B::fb接收三个参数
为了提高可读性和可维护性,我希望我可以这样称呼它:-

std::function<void(int,int,int)> f=std::bind(&B::fb,this);  //omit _1 _2 _3

问题
C++ 中是否有任何功能可以省略占位符?
它应该会自动调用 _1,_2, ..., in orders。

我用谷歌搜索 "omit placeholders c++" 但没有找到任何线索。

您可以创建函数助手(那些是 C++14):

template <class C, typename Ret, typename ... Ts>
std::function<Ret(Ts...)> bind_this(C* c, Ret (C::*m)(Ts...))
{
    return [=](auto&&... args) { return (c->*m)(std::forward<decltype(args)>(args)...); };
}

template <class C, typename Ret, typename ... Ts>
std::function<Ret(Ts...)> bind_this(const C* c, Ret (C::*m)(Ts...) const)
{
    return [=](auto&&... args) { return (c->*m)(std::forward<decltype(args)>(args)...); };
}

然后写

std::function<void(int, int, int)> f = bind_this(this, &B::fb);