了解std :: function和std :: bind [英] Understanding std::function and std::bind

查看:169
本文介绍了了解std :: function和std :: bind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用std :: function和std :: bind玩游戏时,发现一些不直观的东西,我想更好地理解它.

I was playing arround with std::function and std::bind and I noticed something unintuitive and I would like to understand it better.

例如:

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 work

return 0;
}

如何将 function<void(int)> 绑定到 void()的函数. 然后,我可以调用f(1)并获得fun(). 我想了解这是如何完成的. 深入了解Microsoft Visual Studio 2012的实现,这让我迷失了一大堆无法读取的宏.所以这就是为什么我在这里问这个问题.

How is it possible to bind a function<void(int)> to a function that is void(). I could then call f(1) and get fun(). I would like to understand how this is done. Going inside Microsoft Visual Studio 2012's implementation of this got me lost in a sea of unreadable macros. so that is why I ask this question here.

推荐答案

如果不使用参数占位符(_1_2,...),则传递给函数对象的所有参数均从<返回c3>将被丢弃.使用:

If you don't use argument placeholders (_1, _2, ...), then any arguments passed to the function object returned from std::bind will just be discarded. With:

std::function<void(int)> f = std::bind(fun, std::placeholders::_1);

我得到了一个(长而丑陋的)错误.

I get a (long and ugly) error as expected.

对于对Standardese感兴趣的人:

For the people interested in Standardese:

§20.8.9.1.2 [func.bind.bind]

template<class F, class... BoundArgs>
*unspecified* bind(F&& f, BoundArgs&&... bound_args);

p3返回:结果类型弱(20.8.2)的转发呼叫包装器g. g(u1, u2, ..., uM)的效果应为INVOKE(fd, v1, v2, ..., vN, result_of<FD cv (V1, V2, ..., VN)>::type),其中 cv 表示g cv 限定词以及绑定参数的值和类型v1, v2, ..., vN按以下规定确定.

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, v2, ..., vN are determined as specified below.

p10绑定参数v1, v2, ..., vN的值及其对应的类型V1, V2, ..., VN 取决于从对bind cv的调用派生的类型TiD.调用包装器g的em>-限定符 cv 如下:

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

  • 如果TiDreference_wrapper<T>,则参数是tid.get(),其类型ViT&;
  • 如果is_bind_expression<TiD>::value的值为true,则参数为tid(std::forward<Uj>(uj)...),其类型Viresult_of<TiD cv (Uj...)>::type;
  • 如果is_placeholder<TiD>::value的值j不为零,则参数为std::forward<Uj>(uj),其类型ViUj&&;
  • 否则,该值为tid,其类型ViTiD cv &.
  • 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 &.

这篇关于了解std :: function和std :: bind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆