Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-10-22 13:22:38


From: <brownell_at_[hidden]>
> I don't think this is a problem with bind, but more of a C++ issue,
> so my apologies if this is a bit off topic. I am passing a template
> function to bind, and the resulting "bound" function is used as the
> UnaryFunction in std::for_each. When I compile this application,
> everything works as expected, but when the linker does its thing, it
> can't find the template function.

[...]

I assume you are using MSVC.

> std::for_each(v.begin(), v.end(), boost::bind
> (TemplateFunction<unsigned long>, _1));

I was able to get around the problem by using:

bool (*pf) (unsigned long &) = &TemplateFunction<unsigned long>;

std::for_each(v.begin(), v.end(), boost::bind(pf, _1));

This is a good idea in general since "TemplateFunction<unsigned long>" is
not guaranteed to be valid by itself (there is a defect report against
this.)

Another option is to turn the function into a function object:

template<class T> struct F
{
 bool operator()(T & t)
 {
  t = 2;
  return true;
 }
};

and then

    std::for_each(v.begin(), v.end(), boost::bind<bool>(F<unsigned long>(),
_1));

A third option is to use a fancier function object:

struct F
{
 template<class T> bool operator()(T & t)
 {
  t = 2;
  return true;
 }
};

and, correspondingly,

    std::for_each(v.begin(), v.end(), boost::bind<bool>(F(), _1));

Hope this helps. :-)

--
Peter Dimov
Multi Media Ltd.

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