I want to pass an overloaded function to the std::for_each() algorithm. For example,
class A {
void f(char c);
void f(int i);
void scan(const std::string& s) {
std::for_each(s.begin(), s.end(), f);
}
};
I’d expect the compiler to resolve f() by the iterator type. Apparently, it (GCC 4.1.2) doesn’t do it. So, how can I specify which f() I want?
You can use
static_cast<>()to specify whichfto use according to the function signature implied by the function pointer type:Or, you can also do this:
If
fis a member function, then you need to usemem_fun, or for your case, use the solution presented in this Dr. Dobb’s article.