简体   繁体   English

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

[英]omit std::placeholders in std::bind

To create std::function, here is what I do:- 要创建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

It is obvious that B::fb receive three parameters. 很明显, B::fb接收三个参数。
To increase readability & maintainablity, I wish I could call this instead :- 为了提高可读性和可维护性,我希望我可以这样称呼: -

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

Question
Are there any features in C++ that enable omitting the placeholders? C ++中是否有任何功能可以省略占位符?
It should call _1,_2, ..., in orders automatically. 它应该自动在命令中调用_1,_2,....

I have googled "omit placeholders c++" but not find any clue. 我用谷歌搜索“省略占位符c ++”但没有发现任何线索。

You may create functions helper (those ones are C++14): 您可以创建函数帮助程序(那些是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)...); };
}

and then just write 然后写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM