[lambda] logical operators related errors
Hello I have the following testcase: #include <string> #include <iostream> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> struct A { std::string s1, s2, s3; A(std::string const& s1_, std::string const& s2_, std::string const& s3_) :s1(s1_), s2(s2_), s3(s3_) {} }; void eval(bool val) { std::cout << "result: " << val << std::endl; } int main() { using boost::bind; using boost::mem_fn; A a1("str1", "str2", "str3"); A a2("str2", "str1", "str3"); eval(((bind(&A::s1, _1) < bind(&A::s1, _2)) && (bind(&A::s2, _1) < bind(&A::s2, _2)))(a1, a2)); } This errors compiling about operator&& no match found. I am trying to write a binary predicate (for a sort type algorithm) that should lexicographically compare the s1 field of the first argument with the corresponding one in the second argument, then compare the s2 fields similary (in this order). It does seem to work for the simpler case of just one "<" operation but doesn't seem to work when using "&&" on the results of "<". I was hoping there is an overloaded && that works on the functors returned by the overloaded < version. Can someone offer a working solution (possibly better looking than this one heh). Thanks! PS: For didactic reasons I specifically do not want to write the comparator manually but using lambda/bind composition and the like -- Mihai RUSU Email: dizzy@roedu.net GPG : http://dizzy.roedu.net/dizzy-gpg.txt WWW: http://dizzy.roedu.net "Linux is obsolete" -- AST
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Dizzy Sent: Thursday, March 29, 2007 5:05 AM To: boost-users@lists.boost.org Subject: [Boost-users] [lambda] logical operators related errors #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> int main() { using boost::bind; using boost::mem_fn; A a1("str1", "str2", "str3"); A a2("str2", "str1", "str3"); eval(((bind(&A::s1, _1) < bind(&A::s1, _2)) && (bind(&A::s2, _1) < bind(&A::s2, _2)))(a1, a2)); } This errors compiling about operator&& no match found. [Nat] I think the problem may be that you're mixing boost::bind with plain operators. Try using boost::lambda::bind instead and see if you get farther.
Hello On Thursday 29 March 2007 17:42, Nat Goodspeed wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Dizzy Sent: Thursday, March 29, 2007 5:05 AM To: boost-users@lists.boost.org Subject: [Boost-users] [lambda] logical operators related errors
#include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp>
int main() { using boost::bind; using boost::mem_fn;
A a1("str1", "str2", "str3"); A a2("str2", "str1", "str3");
eval(((bind(&A::s1, _1) < bind(&A::s1, _2)) && (bind(&A::s2, _1) < bind(&A::s2, _2)))(a1, a2)); }
This errors compiling about operator&& no match found.
[Nat] I think the problem may be that you're mixing boost::bind with plain operators. Try using boost::lambda::bind instead and see if you get farther.
Ok, using lambda::bind and lambda::_1, lambda::_2 it seems to work. However, I thought that boost::bind since 1.33 version returns a functor that can be used for lambda composition and it's overloaded operators (please excuse me if I just said something stupid, I'm a beginner with all this lambda/bind terms), hence would have explained to me why operator< did work with boost::bind without requiring boost::lambda::bind such as operator&& seems to require. Any idea why operator< works with the boost::bind functors and operator&& doesn't ? Or something else that I'm missing (such as there have been made special overloads for operator< to work with boost::bind functors). Thanks! -- Mihai RUSU Email: dizzy@roedu.net GPG : http://dizzy.roedu.net/dizzy-gpg.txt WWW: http://dizzy.roedu.net "Linux is obsolete" -- AST
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Dizzy Sent: Thursday, March 29, 2007 11:05 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [lambda] logical operators related errors
[Nat] I think the problem may be that you're mixing boost::bind with plain operators. Try using boost::lambda::bind instead and see if you get farther.
Ok, using lambda::bind and lambda::_1, lambda::_2 it seems to work. Any idea why operator< works with the boost::bind functors and operator&& doesn't ? Or something else that I'm missing (such as there have been made special overloads for operator< to work with boost::bind functors). [Nat] IIRC, operator< has a special overload for boost::bind objects (for use as map keys?).
Hello On Thursday 29 March 2007 18:22, Nat Goodspeed wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Dizzy Sent: Thursday, March 29, 2007 11:05 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [lambda] logical operators related errors
[Nat] I think the problem may be that you're mixing boost::bind with plain operators. Try using boost::lambda::bind instead and see if you get farther.
Ok, using lambda::bind and lambda::_1, lambda::_2 it seems to work.
Any idea why operator< works with the boost::bind functors and operator&& doesn't ? Or something else that I'm missing (such as there have been made special overloads for operator< to work with boost::bind functors).
[Nat] IIRC, operator< has a special overload for boost::bind objects (for use as map keys?).
Indeed, I was misinformed initially. The boost bind documentation explains this well (http://www.boost.org/libs/bind/bind.html#operators): "For convenience, the function objects produced by bind overload the logical not operator ! and the relational operators ==, !=, <, <=, >, >=." Thanks for your help. -- Mihai RUSU Email: dizzy@roedu.net GPG : http://dizzy.roedu.net/dizzy-gpg.txt WWW: http://dizzy.roedu.net "Linux is obsolete" -- AST
participants (2)
-
Dizzy
-
Nat Goodspeed