Discussion:
[lambda] Binding template functions
David Greene
2006-01-10 22:11:54 UTC
Permalink
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
Jaakko Jarvi
2006-01-10 22:50:49 UTC
Permalink
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> >
Post by David Greene
&)'
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
Post by David Greene
Is it possible to bind template functions? If so, what's the
#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
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
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Peter Dimov
2006-01-11 02:44:22 UTC
Permalink
Post by David Greene
Is it possible to bind template functions? If so, what's the
#include <boost/lambda/bind.hpp>
#include <boost/numeric/interval.hpp>
#include <vector>
#include <cassert>
typedef boost::numeric::interval<int> range;
[...]
Post by David Greene
// 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.)
David Greene
2006-01-11 17:32:28 UTC
Permalink
Post by Peter Dimov
Post by David Greene
Is it possible to bind template functions? If so, what's the
#include <boost/lambda/bind.hpp>
#include <boost/numeric/interval.hpp>
#include <vector>
#include <cassert>
typedef boost::numeric::interval<int> range;
[...]
Post by David Greene
// 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.)
Yes, that was the problem. Thanks for the explanation.

-Dave

Loading...