[lambda] Binding template functions

Is it possible to bind template functions? If so, what's the syntax? I'm having trouble with this test: #include <boost/lambda/bind.hpp> #include <boost/numeric/interval.hpp> #include <vector> #include <cassert> typedef boost::numeric::interval<int> range; std::vector<range> ranges; int main(void) { using boost::lambda::bind; using boost::lambda::_1; int range_base = 0; int range_limit = 9; for(int i = 0; i < 10; ++i) { ranges.push_back(range(range_base, range_limit)); range_base += 10; range_limit += 10; } std::vector<range>::iterator i = std::find_if(ranges.begin(), ranges.end(), bind(&boost::numeric::template in<range>, 5, _1)); // bind(&boost::numeric::in<range>, 5, _1)); // bind(&boost::numeric::in, 5, _1)); assert(i == ranges.begin()); return(0); } None of the commented options works, either. I wouldn't expect the last one to work. They all fail with the same error trace: lambda.cc: In function `int main()': lambda.cc:27: error: no matching function for call to `bind(<unknown type>, int, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >&)' Any ideas? -Dave

Hi David, Template functions have to be explicitly instantiated if used together with bind. One cannot take the address of an uninstantiated template. The second line bind(&boost::numeric::in<range>, 5, _1)); seems like it should be correct, but I don't know the definition of in. I tried a direct call to boost::numeric::in, however, and it fails: boost::numeric::in<range>(5, *(ranges.begin())); test.cpp:25: error: no matching function for call to 'in(int, boost::numeric::interval<int, boost::numeric::interval_lib::policies<boost::numeric::interval_lib::rou nded_math<int>, boost::numeric::interval_lib::checking_strict<int> >
&)'
So maybe there's something wrong in how you try to call to in? (Or maybe I did a mistake interpreting the call, I was in a rush and couldn't dig very deep on this.) Best, Jaakko Järvi On Jan 10, 2006, at 4:11 PM, David Greene wrote:

David Greene wrote:
[...]
// bind(&boost::numeric::in<range>, 5, _1));
Try bind( boost::numeric::in<range::base_type, range::traits_type>, 5, _1 ) in<> has two template parameters, T and Policies; the & needs to be absent because Lambda has overloads for function references but not for function pointers (the variation with & works with boost::bind, though.)
participants (3)
-
David Greene
-
Jaakko Jarvi
-
Peter Dimov