[boost::bind] How does it works for overloaded member functions in this example?

Hi there! Recently I was extremely surprised by the fact that boost::bind can handle the case of binding overloaded member functions! Here is the simplified testcase, which works fine in gcc 4.4.3. #include <boost/bind.hpp> struct Test { static void Func(int) {}; static void Func(int, const char*) {}; }; int main(int argc, char **argv) { boost::bind(&Test::Func, 5)(); boost::bind(&Test::Func, 5, "WORKS!")(); return 0; } In full testcase i tried to reproduce simplified boost::bind logic, and ensured, that my simplified version is still incorrect: #include <boost/bind.hpp> template <class A1> class Args1 { template <class Fn> void call(Fn fn) { fn(a1); } A1 a1; }; template <class A1, class A2> class Args2 { template <class Fn> void call(Fn fn) { fn(a1, a2); } A1 a1; A2 a2; }; template <class Fn, class Args> struct bind_t { bind_t( Fn fn, Args const& args ) : fn_(fn), args_(args) {} void operator()() { args_.call(fn_); } private : Fn fn_; Args args_; }; template <class Fn, class A1> bind_t< Fn, Args1<A1> > bind(Fn fn, A1 a1) { typedef Args1<A1> args; return bind_t<Fn, args>(args(a1)); } template <class Fn, class A1, class A2> bind_t< Fn, Args2<A1, A2> > bind(Fn fn, A1 a1, A2 a2) { typedef Args2<A1, A2> args; return bind_t<Fn, args>(args(a1, a2)); } struct Test { static void Func(int) {}; static void Func(int, const char*) {}; }; int main(int argc, char **argv) { boost::bind(&Test::Func, 5)(); boost::bind(&Test::Func, 5, "WORKS!")(); bind(&Test::Func, 5)(); bind(&Test::Func, 5, "NOT WORKS!")(); return 0; }

I believe the signature of bind in boost is a bit more complex than in your example. It may go like this: template< class FuncRet, class FuncParam0, class Param0
<unspecified-type> bind( FuncRet (*pf)(FuncParam0), Param0 param0 ) { ... } Since the full signature of the function pointer is provided, the compiler shouldn't have any problems disambiguating. -- Best regards, Alexander Fokin, apfokin@gmail.com.

AMDG On 10/1/2010 5:58 AM, Alexander Fokin wrote:
I believe the signature of bind in boost is a bit more complex than in your example.
It may go like this: template< class FuncRet, class FuncParam0, class Param0> <unspecified-type> bind( FuncRet (*pf)(FuncParam0), Param0 param0 ) { ... }
Since the full signature of the function pointer is provided, the compiler shouldn't have any problems disambiguating.
It can only disambiguate by function arity. If you add a second 1 argument overload, it won't work. In Christ, Steven Watanabe

On Fri, Oct 1, 2010 at 3:42 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 10/1/2010 5:58 AM, Alexander Fokin wrote:
I believe the signature of bind in boost is a bit more complex than in your example.
It may go like this: template< class FuncRet, class FuncParam0, class Param0> <unspecified-type> bind( FuncRet (*pf)(FuncParam0), Param0 param0 ) { ... }
Since the full signature of the function pointer is provided, the compiler shouldn't have any problems disambiguating.
It can only disambiguate by function arity. If you add a second 1 argument overload, it won't work.
I think it would using boost::phoenix::bind though, correct? (Or better, boost::phoenix::function?)
participants (4)
-
Alexander Fokin
-
Chaos A.D.
-
OvermindDL1
-
Steven Watanabe