Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2001-10-07 16:10:44


On Sunday 07 October 2001 01:42, you wrote:
> > So effectively bind can be asked to ignore a return value.
>
> Yes, the workaround has design implications, as you note. But: why would
> you want to ask bind to ignore a return value?

Here's an example: say I want to apply some function object to everything in
a container "c":

for_each(c.begin(), c.end(), myFunctionObject);

"for_each" could be a parallelizing for_each loop, or perhaps could use a
thread pool of some sort. In any case, we end up writing for_each as, perhaps,

template<typename InputIterator, typename F>
void for_each(InputIterator first, InputIterator last, F f)
{
  for (; first != last; ++first) {
    schedule_work(bind<void>(f, *first));
  }

  wait_for_work_to_finish();
}

So in the call to bind, setting the return type equal to void is perfectly
reasonable because we want to ignore the result. The non-parallelizing
version below ignores the result as well:

template<typename InputIterator, typename F>
void for_each(InputIterator first, InputIterator last, F f)
{
  for (; first != last; ++first) {
    f(*first); // ignores return value
  }
}

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk