2

I am trying to define a map from member function pointers to strings in C++. But the the compiler complains that operator < is not defined for member function pointers. Is there a way to do this?

Example code:

#include <map>
#include <string>
class foo;
typedef void (foo::*fooFunc)();

class foo {
public:
  foo() {
    myMap[func]="example function";
  }
  void func() {};
  std::map<fooFunc,std::string> myMap;
};

Resulting error message:

    In file included from C:/msys64/mingw64/include/c++/6.2.0/bits/stl_tree.h:65:0,
                 from C:/msys64/mingw64/include/c++/6.2.0/map:60,
                 from grr.cpp:1:
C:/msys64/mingw64/include/c++/6.2.0/bits/stl_function.h: In instantiation of 'constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = void (foo::*)()]':
C:/msys64/mingw64/include/c++/6.2.0/bits/stl_map.h:501:32:   required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = void (foo::*)(); _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<void (foo::*)()>; _Alloc = std::allocator<std::pair<void (foo::* const)(), std::__cxx11::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::__cxx11::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = void (foo::*)()]'
grr.cpp:9:15:   required from here
C:/msys64/mingw64/include/c++/6.2.0/bits/stl_function.h:386:20: error: invalid operands of types 'void (foo::* const)()' and 'void (foo::* const)()' to binary 'operator<'
       { return __x < __y; }
                ~~~~^~~~~
q.t.f.
  • 133
  • 5

1 Answers1

1

std::map requires a comparison operator for its keys. By default, it uses operator <. Since operator < is not defined for pointer-to-member types, you are getting this error. To fix it you'll need to define a custom comparison operator and provide it as a parameter to std::map. Alternatively, use a different container.

From cppreference:

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare. Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees

If you want options on how to write such a comparison operator, it has already been answered here.

gflegar
  • 1,583
  • 6
  • 22
  • I get that I need a comparison operator, but I can't figure out how to write one for member function pointers. I would have thought there was some way to cast it to an integer type but that doesn't seem to be true. – q.t.f. Apr 14 '18 at 17:15
  • 1
    @q.t.f. I updated the answer, how to do that has already been answered on a different question. However, this is not straightforward, and you may want to consider a different design. – gflegar Apr 14 '18 at 17:25
  • Ugh. Basically the answer is "no" there is no efficient portable way of doing it. – q.t.f. Apr 14 '18 at 17:40