This is a general, skeleton version of a template problem I'm having in C++. I can't quite figure out how to get the bar function template to be recognized as a plausible candidate when invoked from foo.
#include <iostream>
#include <cstdlib>
#include <unordered_map>
template<class T>
std::unordered_map<std::string, T> getStr2TMap() {
return {}; // suppose logic here is a bit more involved
}
template<class T>
std::unordered_map<int, T> getInt2TMap() {
return {}; // suppose logic here is a bit more involved
}
template<class U, class T>
void bar(
const std::function<void (std::unordered_map<U, T>&&)>& baz
) {
if (rand() % 2 > 0) {
baz(getInt2TMap<T>());
} else {
baz(getStr2TMap<T>());
}
}
template<class T>
void foo(
const std::unordered_map<std::string, T>& map1
) {
bar([&map1](auto&& map2) {
// do some things with map1 and map2
});
}
int main() {
std::unordered_map<std::string, int> myMap;
foo<int>(myMap);
}
EDIT
Much simplified version of the code, same error. I'm looking for solutions to the above version though, not this one.
#include <iostream>
#include <functional>
#include <unordered_map>
template<class T>
void foo(
const std::function<void (std::unordered_map<int, T>&&)>& bar
) {
std::unordered_map<int, T> myMap;
bar(myMap);
}
int main() {
foo([](auto&& m) {
});
}
foo? Is there any way to deduce it?