You can do something like this when the type of map is inferred from the function you pass to the function.
#include <map> template<class Key, class Value, class F> std::map<Key, Value, F> make_map(const F& f) { return std::map<Key, Value, F>{f}; } int main() { auto my_map = make_map<int, int>([](const int&a, const int& b) { return a < b; }); my_map[10] = 20; }
I do not see the point in this, but I will not say that it is useless. Typically, you need a well-known comparator to easily carry the card. With the above setting, you always turn off using the template functions as shown below.
tempalte<class F> void do_somthing(const std::map<int, int, F>& m) { }
This is not necessarily bad, but my instincts tell me that having a type that ONLY can be viewed using common functions is bad. I think this works great for lambda functions, but more on that. The solution here is to use std :: function
#include <map> #include <functional> template<class Key, class Value> using my_map_t = std::map<Key, Value, std::function<bool(const Key&, const Key&)>>; int main() { my_map_t<int, int> my_map{[](const int&a, const int& b) { return a < b; }}; my_map[10] = 20; }
Now you can use any predicate you want, and you have a specific type to work with, my_map
hope this helps!