I am currently working on a program, and I want to pass a function pointer to a map for a custom comparator. In the following minimum, verifable, example, however, this produces errors:
#include <iostream>
#include <map>
struct CustomKey{
unsigned a;
};
bool compareCustom(const CustomKey &a, const CustomKey &b){
return a.a < b.a;
}
typedef decltype(compareCustom) CustomComparator;
int main(){
std::map<CustomKey, unsigned, CustomComparator> customMap(&compareCustom);
return 0;
}
Compiling the above code with either GCC or Clang produces a plethora of uninformative template errors, entirely centered around the internal implementation of std::map
. This question seems to suggest that passing a function pointer type is perfectly valid. What is the issue with my code?