相关疑难解决方法(0)

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

我有一个指针容器,我想迭代,调用一个成员函数,其参数是一个参考.如何使用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)
);
Run Code Online (Sandbox Code Playgroud)

一个相关的问题(我从中派生出我当前的解决方案)是boost :: bind,其函数的参数是引用.这特别询问如何使用boost进行此操作.我问如何在没有提升的情况下完成.

编辑:有一种方法可以做同样的事情,而不使用任何boost.通过使用std::bind和朋友相同的代码可以在C++ 11兼容的编译器中编写和编译,如下所示:

std::for_each(
  sprites.begin(),
  sprites.end(),
  std::bind(&Renderable::render, std::placeholders::_1, std::ref(g))
);
Run Code Online (Sandbox Code Playgroud)

c++ stl pass-by-reference

8
推荐指数
1
解决办法
7354
查看次数

boost :: bind()是通过引用还是按值复制参数?

为什么valgrind的DRD工具抱怨"线程加载冲突...大小为4":关于这样的代码:

void SomeFunction(const int& value)
{
    boost::bind(..., value); /* <-- complaines on this line
                                with last backtrace function "new(int)" */
}
Run Code Online (Sandbox Code Playgroud)

boost :: bind()是否按引用或值存储值?

c++ valgrind boost-bind

6
推荐指数
1
解决办法
5501
查看次数

标签 统计

c++ ×2

boost-bind ×1

pass-by-reference ×1

stl ×1

valgrind ×1