c – 了解std :: function和std :: bind

c – 了解std :: function和std :: bind,第1张

概述我正在玩std :: function和std :: bind,我注意到一些不直观的东西,我想更好地了解它. 例如: void fun(){}void hun(std::string) { }int main(){ function<void(int)> g = &fun; //This fails as it should in my understanding. 我正在玩std :: function和std :: bind,我注意到一些不直观的东西,我想更好地了解它.

例如:

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的这个实现让我失去了一大堆不可读的宏.所以我在这里问这个问题.

解决方法 如果不使用参数占位符(_1,_2,…),则传递给从std :: bind返回的函数对象的任何参数都将被丢弃.附:
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 g with a weak result type (20.8.2). The effect of g(u1,u2,...,uM) shall be INVOKE(fd,v1,v2,vN,result_of<FD cv (V1,V2,VN)>::type),where cv represents the cv-qualifIErs of g and the values and types of the bound arguments v1,vN are determined as specifIEd below.

p10 The values of the bound arguments v1,vN and their corresponding types V1,VN depend on the types TID derived from the call to bind and the cv-qualifIErs cv of the call wrapper g as follows:

if TID is reference_wrapper<T>,the argument is tID.get() and its type Vi is T&; if the value of is_bind_Expression<TID>::value is true,the argument is tID(std::forward<Uj>(uj)...) and its type Vi is result_of<TID cv (Uj...)>::type; if the value j of is_placeholder<TID>::value is not zero,the argument is std::forward<Uj>(uj) and its type Vi is Uj&&; otherwise,the value is tID and its type Vi is TID cv &.
总结

以上是内存溢出为你收集整理的c – 了解std :: function和std :: bind全部内容,希望文章能够帮你解决c – 了解std :: function和std :: bind所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1254283.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存