I know this question has been asked before, but despite being a fairly experienced coder I don't understand the answers and see no way to respond to these previous questions to ask for clarification. There is no "reply" link or anything. Besides, those questions were quite old. So, I'm asking the question afresh.
I have a class, in which I am overloading the += operator. I want one overload to take a bare function pointer, and the other to take a std::function:
void operator+=(void (*handler)());
void operator+=(function<void (void *, T)> handler);
Usage:
MyClass a;
a += [](){ DoSomething(); };
a += [](void *x, T y){ DoSomething(); };
Unfortunately, the code does not compile because the compiler cannot determine that the second overload is unsuitable for the first += call.
How do I define my operator+= member functions to correct this problem? I don't want to change how the operators are used (e.g. by using an explicit cast). I want them to work as demonstrated above.
In addition, it would be handy to have the following overload as well:
void operator+=(function<void()> handler);
But again, I can't overload based on the signature of the function<> template.
See this thread for further examples: Isn't the template argument (the signature) of std::function part of its type? (I tried implementing the various solutions mentioned in that thread, and none of them would compile)
I've been programming for many years in a number of languages, but my C++ skills are a little rusty.