Boost logo

Boost Users :

Subject: Re: [Boost-users] Why doesn't boost::function use get_pointer() ?
From: Peter Soetens (peter.soetens_at_[hidden])
Date: 2010-09-15 03:26:20


On Tuesday 14 September 2010 23:47:41 Steven Watanabe wrote:
> AMDG
>
> Peter Soetens wrote:
> > When using boost::bind, I can write this:
> >
> > struct X
> > {
> > void foo(int) {}
> > };
> >
> > shared_ptr<X> x( new X );
> > // bind takes shared_ptr:
> > bind(&X::foo, x, 3)();
> >
> > But I can't write this:
> >
> > function<void(X*,int)> foo = &X::foo;
> > // function refuses shared_ptr:
> > foo(x,1);
> >
> > Why doesn't boost::function have the same get_pointer() trick ?
>
> Overload resolution for calling a boost::function is exactly the
> same as for calling a function with that signature. shared_ptr<X>
> is not convertible to X*, so the call fails.

So this is by design I presume.

>
> > Or how can I
> > rewrite the function signature such that it does work with shared
> > pointers ?
>
> function<void(const shared_ptr<X>&, int)> foo;

Thank you. The problem was that I didn't know if the first argument was a
regular pointer or a shared pointer (generic code!). I have also found another
solution, that is, by 'always' writing the call like this:

  foo(get_pointer(x),1);

Which will work for all cases of x, and the signature of foo can remain
void(X*,int).

Peter


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