
例如:
voID fun(){}voID hun(std::string) { }int main(){ function<voID(int)> g = &fun; //This fails as it should in my understanding. function<voID(int)> f = std::bind(fun); //This works for reasons unkNown to me function<voID(int,std::string)> h = std::bind(hun); //this doesn't workreturn 0;} 如何绑定函数< voID(int)>到一个voID()函数.
我可以调用f(1)并获得乐趣().
我想了解如何做到这一点.
进入Microsoft Visual Studio 2012的这个实现让我失去了一大堆不可读的宏.所以我在这里问这个问题.
std::function<voID(int)> f = std::bind(fun,std::placeholders::_1);
我按预期得到一个(长而丑)的错误.
对于对Standardese感兴趣的人:
§20.8.9.1.2[func.bind.bind]
template<class F,class... BoundArgs>*unspecifIEd* bind(F&& f,BoundArgs&&... bound_args);
总结p3 Returns: A forwarding call wrapper
gwith a weak result type (20.8.2). The effect ofg(u1,u2,...,uM)shall beINVOKE(fd,v1,v2,vN,result_of<FD cv (V1,V2,VN)>::type),where cv represents the cv-qualifIErs ofgand the values and types of the bound argumentsv1,vNare determined as specifIEd below.p10 The values of the bound arguments
ifv1,vNand their corresponding typesV1,VNdepend on the typesTIDderived from the call tobindand the cv-qualifIErs cv of the call wrappergas follows:TIDisreference_wrapper<T>,the argument istID.get()and its typeViisT&; if the value ofis_bind_Expression<TID>::valueistrue,the argument istID(std::forward<Uj>(uj)...)and its typeViisresult_of<TID cv (Uj...)>::type; if the valuejofis_placeholder<TID>::valueis not zero,the argument isstd::forward<Uj>(uj)and its typeViisUj&&; otherwise,the value istIDand its typeViisTID cv &.
以上是内存溢出为你收集整理的c – 了解std :: function和std :: bind全部内容,希望文章能够帮你解决c – 了解std :: function和std :: bind所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)