I have the following code sample:
template<typename T>
void print(T t) {
std::cout << t << std::endl;
}
template<typename Key, typename Value, typename F>
void traverse(std::map<Key, Value>& m, F f) {
for (auto&& [key, value] : m) {
f(value);
}
}
int main()
{
std::map<int, std::string> M;
// initializing the map
traverse(M, print);
}
Here the compiler erred with the following message:
could not deduce template argument for 'F'
The explicit traverse(M, print<std::string>); solves the problem.
My question really is why compiler cannot deduce the template type for the print function?
As far as I understand, all the compile time information is available.