c++ - Why use bind instead of function call? -
what advantage of using boost::bind in case
std::for_each(participants_.begin(), participants_.end(), boost::bind(&chat_participant::deliver, _1, boost::ref(msg)));
instead of
for(iterator actual = participants_.begin(); actual != participants_.end(); ++actual) (*actual)->deliver(msg);
link whole code (this simple server example provided boost tutorials).
i think it's pre c++11, algorithms recommended on plain for-loops things. in theory, it's easier understand purpose of code, don't have understand whole loop implementation first. for_each perhaps extreme though, since for-loop implementation simplest.
before lambda functions, 'boost::bind' requirement if wanted use algorithm without defining custom functor.
nowadays, range-based loops, you'd this:
for (auto& participant : participants) participant->deliver(msg);
algorithms still better more complicated loops though (especially don't have use 'boost::bind').
Comments
Post a Comment