省略std::bind中的std::占位符
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
显然,CCD_ 2接收三个参数
为了增加可读性&可维护性,我希望我可以称之为:-
std::function<void(int,int,int)> f=std::bind(&B::fb,this); //omit _1 _2 _3
问题
C++中有什么功能可以省略占位符吗
它应该调用_1
,_2
。。。,订单中自动。
我在谷歌上搜索了";省略占位符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);
相关文章:
- 将模板与类模板扣除占位符参数匹配
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- Tensorflow C++ 占位符初始化
- std::bind 不会接受 bind 占位符的 std::cref - 为什么?
- qt 中的占位符
- std::绑定可变参数模板、绑定参数和占位符
- 在另一个函数中使用std::bind返回对象和占位符作为参数
- 是否可以在 c++11 中扩展 std::tie 以接受占位符?
- C++std::cin制作一个占位符
- std::bind 占位符的重载运算符
- 将带有占位符的绑定结果存储在 std::函数中
- 如何在视觉C++中与 4 个以上的占位符进行 std::bind
- G++ 错误:“占位符”不是使用命名空间 std::p laceholders; 的命名空间名称
- std::函数回调,参数采用观察者模式(寄存器主题上的占位符)
- 在对象中存储一个std::函数,该函数包括std::占位符
- 为什么 std::bind 的占位符不使用非类型模板参数实现?
- 省略std::bind中的std::占位符
- 为什么在这种情况下std::bind中需要占位符
- 无法从 Boost.Spirit 中的占位符构造 std::string
- 为什么在这个例子(成员函数)中没有占位符std::bind不能工作?