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; }
                ~~~~^~~~~

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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. 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.
Ugh. Basically the answer is "no" there is no efficient portable way of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.