C++ std::for_each,使用引用参数调用成员函数

C++ std::for_each,使用引用参数调用成员函数,c++,stl,pass-by-reference,C++,Stl,Pass By Reference,我有一个指针容器,我想迭代它,调用一个成员函数,该函数有一个作为引用的参数。如何使用STL实现这一点 我当前的解决方案是使用boost::bind和boost::ref作为参数 // Given: // void Renderable::render(Graphics& g) // // There is a reference, g, in scope with the call to std::for_each // std::for_each( sprites.begin(),

我有一个指针容器,我想迭代它,调用一个成员函数,该函数有一个作为引用的参数。如何使用STL实现这一点

我当前的解决方案是使用boost::bind和boost::ref作为参数

// Given:
// void Renderable::render(Graphics& g)
//
// There is a reference, g, in scope with the call to std::for_each
//
std::for_each(
  sprites.begin(),
  sprites.end(),
  boost::bind(&Renderable::render, boost::ref(g), _1)
);
一个相关的问题(我从中得出当前的解决方案)是。这专门询问如何使用boost实现这一点。我想问的是,如果没有助推,它将如何实现

编辑:有一种方法可以在不使用任何
boost的情况下执行相同的操作。通过使用
std::bind
和friends,可以在与C++11兼容的编译器中编写和编译相同的代码,如下所示:

std::for_each(
  sprites.begin(),
  sprites.end(),
  std::bind(&Renderable::render, std::placeholders::_1, std::ref(g))
);

这是
设计中的一个问题。您必须使用boost::bind或tr1::bind。

是的,很遗憾:(关于这一点的更多信息:这显然从未进入标准。但在下一个标准中:)是的,引用问题正是我的问题。谢谢你的信息!boost::bind(&Renderable::render,_1,boost::ref(g))应该是boost::bind(&Renderable::render,boost::ref(g),_1)@carleeto Good catch。经过4年的错误修正!