绑定到成员变量

发布于 2024-07-27 15:00:12 字数 822 浏览 1 评论 0原文

我对绑定到成员变量时 boost::bind 的作用感到困惑。 通过绑定到成员函数,我们本质上创建了一个函数对象,然后调用它并向其传递通过占位符提供或延迟和替换的参数。

但是这个表达式在幕后做了什么:

boost::bind(&std::pair::second, _1); 

用什么来代替占位符 _1?

我在阅读有关 boost::bind 的文章中的示例时发现了这一点:

void print_string(const std::string& s) {
  std::cout << s << '\n';
}

std::map<int,std::string> my_map;
my_map[0]="Boost";
my_map[1]="Bind";

std::for_each(
  my_map.begin(),
  my_map.end(),
   boost::bind(&print_string, boost::bind(
      &std::map<int,std::string>::value_type::second,_1)));

来源

I am confused as to what boost::bind does when we bind to member variables. With binding to member function, we essentially create a function object, and then call it passing to it the arguments that are provided or delayed and substituted via placeholders.

But what does this expression do behind the scenes:

boost::bind(&std::pair::second, _1); 

What gets substituted in place of the placeholder _1?

I found this while reading this example from an article on boost::bind:

void print_string(const std::string& s) {
  std::cout << s << '\n';
}

std::map<int,std::string> my_map;
my_map[0]="Boost";
my_map[1]="Bind";

std::for_each(
  my_map.begin(),
  my_map.end(),
   boost::bind(&print_string, boost::bind(
      &std::map<int,std::string>::value_type::second,_1)));


source

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2024-08-03 15:00:13

它在幕后使用成员指针并将其应用于传入的参数。 在绑定上下文中它相当复杂,因此这里是一个指向成员使用的指针的简单示例:

int main()
{
   std::pair< int, int > p1 = make_pair( 1, 2 );
   std::pair< int, int > p2 = make_pair( 2, 4 );
   int std::pair<int,int>::*ptr = &std::pair<int,int>::second; // pointer to second member

   std::cout << p1.*ptr << std::endl; // 2 dereference pointer to member 
   std::cout << p2.*ptr << std::endl; // 4 dereference pointer to member
}

绑定在幕后组合不同的调用。 生成的函子采用 std::map<>::iterator 的取消引用(类型为 std::pair< const key_type, value_type >)。 它被传递到内部绑定,该内部绑定取消引用成员指针,从而将 (*i​​t).second 返回到外部绑定,该外部绑定将该值传递给 print_string 方法最终调用:print_string( (*it).second )

(*i​​t) 实际上是您询问的 _1。 所有 _# 都是占位符,也就是说,bind 的结果将是一个函子,该函子将采用与占位符数量定义的顺序存在的不同占位符一样多的参数。 在您给出的示例中,生成的函子采用单个参数_1

Behind the scenes it is using a member pointer and applying it to the passed in argument. It is quite complex in the context of binds, so here is a simple example of pointer to member usage:

int main()
{
   std::pair< int, int > p1 = make_pair( 1, 2 );
   std::pair< int, int > p2 = make_pair( 2, 4 );
   int std::pair<int,int>::*ptr = &std::pair<int,int>::second; // pointer to second member

   std::cout << p1.*ptr << std::endl; // 2 dereference pointer to member 
   std::cout << p2.*ptr << std::endl; // 4 dereference pointer to member
}

Behind the scenes bind is composing different calls. The resulting functor takes the dereference of std::map<>::iterator (of type std::pair< const key_type, value_type >). That is passed to the inner bind, that dereferences the member pointer, thus returning (*it).second to the outer bind that passes that value to the print_string method for the final call: print_string( (*it).second ).

(*it) is actually the _1 you asked about. All _# are placeholders, that is, the result of bind will be a functor that will take as many arguments as different placeholders exist in the order defined by the number of placeholder. In the example you gave, the resulting functor takes a single argument _1.

姐不稀罕 2024-08-03 15:00:13
boost::bind(&std::pair<U,V>::second, _1);

功能上等同于

std::pair<U,V> p -> p.second

ie。 它是一个将映射到其第二个成员的函数(对象)。

boost::bind(&std::pair<U,V>::second, _1);

is functionally equivalent to

std::pair<U,V> p -> p.second

ie. it is a function (object) that maps a pair<U,V> to its second member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文