
Hi, I have the following code: bool do_something_conditional(int *handle, const boost::function<bool(int)> &pred); bool pred_a(int handle); bool pred_b(int handle, int n1, int n2); bool do_something(int *handle, int n1, int n2) { return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), boost::not1(&pred_a), boost::bind(&pred_b, _1, n1, n2))); } But it does not compile. I have been not playing with bind stuff for some time. But I've reviewed the doc and some of my old 'bind' code, it seems correct to me. The error message, you know, is quite complicated for me to find a direct answer. Could anybody please help me? Thanks in advance. I'm using 1.39 with MS VS2008SP1 Best regards Max

On Sunday, May 27, 2012 05:40 PM, Max wrote:
Hi,
I have the following code:
bool do_something_conditional(int *handle, const boost::function<bool(int)> &pred); bool pred_a(int handle); bool pred_b(int handle, int n1, int n2);
bool do_something(int *handle, int n1, int n2) { return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), boost::not1(&pred_a), boost::bind(&pred_b, _1, n1, n2))); }
But it does not compile.
This should work and is probably clearer: return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), !boost::bind(&pred_a, _1), boost::bind(&pred_b, _1, n1, n2))); Ben

I have the following code:
bool do_something_conditional(int *handle, const boost::function<bool(int)> &pred); bool pred_a(int handle); bool pred_b(int handle, int n1, int n2);
bool do_something(int *handle, int n1, int n2) { return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), boost::not1(&pred_a), boost::bind(&pred_b, _1, n1, n2))); }
But it does not compile.
This should work and is probably clearer:
return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), !boost::bind(&pred_a, _1), boost::bind(&pred_b, _1, n1, n2)));
Yes, it works. Thank you, Ben. But why is my way wrong? With either boost::not1(&pred_a) or !boost::bind(&is_master_device, _1) we get a functor that takes 1 parameter of type int. Max

On Sunday, May 27, 2012 08:12 PM, Max wrote:
I have the following code:
bool do_something_conditional(int *handle, const boost::function<bool(int)> &pred); bool pred_a(int handle); bool pred_b(int handle, int n1, int n2);
bool do_something(int *handle, int n1, int n2) { return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), boost::not1(&pred_a), boost::bind(&pred_b, _1, n1, n2))); }
But it does not compile.
This should work and is probably clearer:
return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), !boost::bind(&pred_a, _1), boost::bind(&pred_b, _1, n1, n2)));
Yes, it works.
Thank you, Ben.
But why is my way wrong? With either boost::not1(&pred_a) or !boost::bind(&is_master_device, _1) we get a functor that takes 1 parameter of type int.
I don't know. I think there is some kind of voodoo in boost::bind that means the outer bind will evaluate the inner bind, bot won't try to evaluate not1. It would be interesting to see what std::bind does, it tends to have less voodoo. Ben

I have the following code:
bool do_something_conditional(int *handle, const boost::function<bool(int)> &pred); bool pred_a(int handle); bool pred_b(int handle, int n1, int n2);
bool do_something(int *handle, int n1, int n2) { return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), boost::not1(&pred_a), boost::bind(&pred_b, _1, n1, n2))); }
But it does not compile.
This should work and is probably clearer:
return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), !boost::bind(&pred_a, _1), boost::bind(&pred_b, _1, n1, n2)));
Yes, it works.
Thank you, Ben.
But why is my way wrong? With either boost::not1(&pred_a) or !boost::bind(&is_master_device, _1) we get a functor that takes 1 parameter of type int.
I don't know. I think there is some kind of voodoo in boost::bind that means the outer bind will evaluate the inner bind, bot won't try to evaluate not1.
The "voodoo" is described here: http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#nested_binds Basically, bind treats nested binds specially, precisely to allow the sort of thing the OP's trying to do. Regards, Nate

Ben Pope wrote:
This should work and is probably clearer:
return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), !boost::bind(&pred_a, _1), boost::bind(&pred_b, _1, n1, n2)));
Or even return do_something_conditional(handle, !boost::bind(&pred_a, _1) && boost::bind(&pred_b, _1, n1, n2) ); As for the original error, error C2664: 'bool std::logical_and<_Ty>::operator ()(const _Ty &,const _Ty &) const' : cannot convert parameter 1 from 'boost::unary_negate<Predicate>' to 'const bool &' the compiler is right, logical_and doesn't accept boost::not1(&pred_a) as a first argument, it takes a bool. To make it compile, one needs to wrap boost::not1(&pred_a) in a boost::bind, as follows: bool do_something(int *handle, int n1, int n2) { return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), boost::bind(boost::not1(&pred_a), _1), boost::bind(&pred_b, _1, n1, n2))); } This will call boost::not1(&pred_a) with _1 instead of passing it as-is to logical_and.

Ben Pope wrote:
Or even
return do_something_conditional(handle, !boost::bind(&pred_a, _1) && boost::bind(&pred_b, _1, n1, n2) );
As for the original error,
error C2664: 'bool std::logical_and<_Ty>::operator ()(const _Ty &,const _Ty &) const' : cannot convert parameter 1 from 'boost::unary_negate<Predicate>' to 'const bool &'
the compiler is right, logical_and doesn't accept boost::not1(&pred_a) as a first argument, it takes a bool. To make it compile, one needs to wrap boost::not1(&pred_a) in a boost::bind, as follows:
bool do_something(int *handle, int n1, int n2) { return do_something_conditional(handle, boost::bind(std::logical_and<bool>(), boost::bind(boost::not1(&pred_a), _1), boost::bind(&pred_b, _1, n1, n2))); }
This will call boost::not1(&pred_a) with _1 instead of passing it as-is to logical_and.
Thank you for your swiss army knife, Peter. Max
participants (4)
-
Ben Pope
-
Max
-
Nathan Ridge
-
Peter Dimov