Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost lambda bind] overloading not compiling
From: Kaz (sfx810_at_[hidden])
Date: 2012-04-13 07:23:51


Thanks every one for help. Does that mean I should avoid using
overloaded functions with both of bind and lambda bind ?

Regards
Kaz.

On Fri, Apr 13, 2012 at 6:41 AM, Thomas Heller
<thom.heller_at_[hidden]> wrote:
> On 04/13/2012 03:07 AM, Jeffrey Lee Hellrung, Jr. wrote:
>>
>> On Thu, Apr 12, 2012 at 3:02 PM, Kaz<sfx810_at_[hidden]>  wrote:
>>
>>> Hi All, I came across this strange error. Binding of overloaded member
>>> functions works with boost bind, but not with boost lambda bind. Here
>>> is the example
>>>
>>> Following builds and works fine
>>>
>>> #include "boost/function.hpp"
>>> #include "boost/bind.hpp"
>>>
>>> class Operations
>>> {
>>> public:
>>>        void Fun1( int i ){ std::cout<<"\nfunction with one arg\n";     }
>>>        void Fun1( int i, double d)     {std::cout<<"\nfunction with two
>>> args\n"; }
>>> };
>>> -----------------------
>>> int main()
>>> {
>>>        Operations op;
>>>        typedef boost::function<void ()>   fptr;
>>>        fptr func1 = boost::bind(&Operations::Fun1,&op,1);
>>>        fptr func2 = boost::bind(&Operations::Fun1,&op, 1, 2.2 );
>>>              return 0;
>>> }
>>>
>> I'm kind of surprised this works. Perhaps bind infers the correct arity
>> given the number of parameters, and based on the arity, the compiler is
>> able to deduce which overload of Fun1 you want. In any case, it probably
>> wouldn't work if you overloaded Fun1 on argument type only.
>
> Correct. Boost.Bind is deducing the correct function pointer. Your
> assumption is correct that when overloading only on one argument, it fails.
> I was as surprised as you are, however, this works for phoenix too. If the
> OP really needs more functionality than provided by bind, he should go with
> phoenix.
>>
>>
>>
>>> ------------------------
>>> While following Does not compile
>>>
>>> int main()
>>> {
>>>        using namespace boost::lambda;
>>>        Operations op;
>>>        typedef boost::function<void ()>   fptr;
>>>        fptr func1 = bind(&Operations::Fun1,&op,1);
>>>        fptr func2 = bind(&Operations::Fun1,&op, 1, 2.2 );
>>>              return 0
>>> }
>>>
>>> -------------------------
>>>
>>> This spits out massive error, as shown below. I think gist of which is
>>> it can't figure out which function to instantiate, when using lambda
>>> bind. Can some body please point out what am I missing here ?
>>>
>> I'm guessing that, unlike boost::bind, boost::lambda::bind is not
>> overloaded directly for member function pointers. In any case, you're
>> probably asking for trouble with expressions like&Operations::Fun1 when
>> Fun1 is overloaded :/
>>
>>
>>> (Sorry for long email, I didn't wanted to clutter the email with this
>>> error message, bu thought most of you will figure it out just from
>>> looking at it.).
>>> Any help will be appreciated, and thanks in advance.
>>> Kaz
>>>
>>> boostFunctionAndBind.cpp: In function ‘int main()’:
>>> boostFunctionAndBind.cpp:61:45: error: no matching function for call
>>> to ‘bind(<unresolved overloaded function type>, Operations*, int)’
>>> boostFunctionAndBind.cpp:61:45: note: candidates are:
>>>
>> [...snip candidates...]
>>
>> - Jeff
>>
>>
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net