Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost lambda bind] overloading not compiling
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-04-12 21:07:23


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.

> ------------------------
> 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 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