I have two overloaded member functions with the following signatures:
class MyClass
{
void f(int, int, int);
void f(int, int, int, double);
};
I am using boost::bind as follows:
boost::bind(&MyClass::f, _1, 1, 2, 3); // _1 is a placeholder for the implicit parameter
My problem is actually that there is no problem. According to boost::bind documentation (at http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#err_overloaded) this should “usually” result in an error, and I should need to cast to the function pointer type. However my code compiles without error, and appears to run as expected.
The example given in the documentation is one where the only difference in the overloaded functions is that one is const while the other is not. My guess therefore is that I do not have a problem because the compiler can tell the difference between the two overloaded functions due to the fact that the number (and types) of arguments is different, whereas in the documentation’s example there is no way for the compiler to tell which version you intend simply from the arguments passed to boost::bind. On the other hand, I am skeptical of my guess because I question how the compiler knows that the last 3 arguments passed to boost::bind in my example are linked to the function pointer in the first argument (and therefore form part of its signature) – it seems to me that that is an internal matter for boost::bind which the compiler should have no knowledge of.
Neither the documentation nor any other advice I can find upon googling this issue specify that there is only an issue with overloaded functions which have the same argument types. So, I would be grateful if anyone could confirm that my guess is correct (and by implication, why my skepticism is wrong), before I begin relying on my code under the (possibly false) assumption that it is valid. My concern is that the compiler is simply choosing which function to bind based on reasoning which I did not intend e.g. picking the first one it comes across.
If there is only one choose that is semantically correct, then you are fine (like in your example). If there are more, don’t guess what it will pick, pick for it (like in boost’s example).
Since bind, like everything else in c++, is strongly typed, the compiler can’t just shrink the function or something so it will not make impossible chooses like picking the first in your example.