Boost logo

Boost :

From: David Brownell (brownell_at_[hidden])
Date: 2001-10-22 13:14:35


Excellent, the first option will work in this situation. Thanks for all the
help!

-----Original Message-----
From: Peter Dimov [mailto:pdimov_at_[hidden]]
Sent: Monday, October 22, 2001 11:23 AM
To: boost_at_[hidden]
Subject: Re: [boost] bind, template functions, and the linker

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.
Info: http://www.boost.org  Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]> 
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



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