I defined a function like this, in which there is a template template class
template cl
You don't really need template template parameter in the first place. You could make it
template<typename Map>
struct ForEachOf {
void operator()(const Map& map,
std::function<void (typename Map::key_type, typename Map::mapped_type)> func) const {
for(const auto& pair : map) {
func(pair.first, pair.second);
}
}
};
or even
template <typename F>
void operator() (const Map& map, F func) const { ... }
Your problem is that ::std::map does not take only two template parameters.
The solution is simply to add a template that takes only two parameters instead of four:
template<typename key, typename value>
using mymap = std::map<key, value>;
(See it)
Or, alternatively add the missing arguments with their defaults:
template<typename Key, typename Value, template <typename, typename, typename, typename> class Map>
struct ForEachOf {
void operator()(const Map<Key, Value, ::std::less<Key>, ::std::allocator<std::pair<const Key, T> >>& map, std::function<void (Key, Value)> func) {
for(const auto& pair : map) {
func(pair.first, pair.second);
}
}
};
(See it)
Which can similarly be written by using a variadic type template:
template<typename Key, typename Value, template <typename...> class Map>
struct ForEachOf {
void operator()(const Map<Key, Value>& map, std::function<void (Key, Value)> func) {
for(const auto& pair : map) {
func(pair.first, pair.second);
}
}
};
(See it)
Of course, you could also just create a template function that takes the map and deduces everything:
#include <map>
#include <string>
#include <iostream>
template<typename T, typename F>
void ForEachOf(T&& map, F&& func) {
for(auto& pair : map) {
func(pair.first, pair.second);
}
}
int main(void) {
std::map<int, std::string> m { {1, "foo"}, {3, "bar"}};
ForEachOf(m, [](auto key, auto value) {
::std::cout << key << value;
});
}
(See it)