top

boost::binders and function overloads

The quote and the code-snippet below is taken from Jaako Järvi and Gary Powell.

"Note, that in C++ it is possible to take the address of an overloaded function only if the address is assigned to, or used as an initializer of, a variable, the type of which solves the amibiguity, or if an explicit cast expression is used. This means that overloaded functions cannot be used in bind expressions directly, e.g.:"

void foo(int);
void foo(float);
int i; 
  ...
bind(&foo, _1)(i);                               // error 
  ...
void (*pf1)(int) = &foo;
bind(pf1, _1)(i);                                // ok 1
bind(static_cast<void(*)(int)> (&foo), _1)(i);   // ok 2
If one might want to use an overloaded (member-) function with a binder an explicit disambiguation is needed to tell the compiler which overload it should take. Unfortunately unwieldy. Also, const and non-const member-functions need explicit disambiguation.