I have a map of std::unique_ptr of type foo, and I’m trying to iterate the map, passing the value of each to a public member of a foo not in the map. I’m close, but I can’t figure out how turn the result of the inner most bind from a reference to a pointer.
Given:
class foo
{
public:
bool DoWork(foo *);
};
std::map<long, std::unique_ptr<foo> map_t;
map_t foo_map_;
foo bar_;
std::for_each(std::begin(foo_map_), std::end(foo_map_), std::bind(&Foo::DoWork, &bar_, std::bind(&std::unique_ptr<foo>::get, /* This guy here -->*/std::bind(&std::pair<long, std::unique_ptr<foo>>::second, std::placeholders::_1))));
Recommendations? I’m using Visual Studio 2010 SP1.
The compiler error is due to the fact that
std::mapconst’ifies the key type, so that thevalue_typeofmap_tis notstd::pair<long, std::unique_ptr<foo> >but ratherstd::pair<long const, std::unique_ptr<foo> >. To avoid such errors prefer usingmap_t::value_type. The following change fixes the error:In C++11 you can simplify it to:
Or, using
std::for_eachand C++11 lambda: