This code doesn't work, because &f
creates a function pointer to only one of the the two f()
functions, so the compiler doesn't know which one:
#include <iostream>
#include <string>
void f(int i) {
std::cout << "f(int)" << std::endl;
}
void f(std::string str) {
std::cout << "f(std::string)" << std::endl;
}
template<typename Callback>
void call(const Callback& callback) {
callback(1);
callback("a");
}
int main() {
call(&f);
}
It works as intended if I instead make a template lambda function like
call([](const auto& arg) { f(arg); });
Is there a way to do this without writing an extra lambda, that needs to pass all the arguments? For example with a std::bind
expression? Or is there a limitation in the language that the overload for f
only gets resolved in a function call expression?