0

For some reason I get this error message

invalid operands of types 'void (S::* const)()' and 'void (S::* const)()' to binary 'operator<'

for this code snippet:

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>

struct S
{
    void f() {}
};

typedef void(S::*tdef)();

int main()
{
    boost::tuple<tdef> t1(&S::f);
    boost::tuple<tdef> t2(&S::f);
    return t1 < t2;
}

Boost documents are very tight-lipped on using member function pointers in tuples (apart from they are valid elements), so I don't really have a clue what could be the problem or how those 'const' qualifiers got into the expression.

Any hint?

Rathelor
  • 3
  • 1
  • 1
    What do you want to achieve with that `operator<`? Member function pointers in c++ are not relationally comparable. – SingerOfTheFall Oct 22 '15 at 08:58
  • 3
    `boost::tuple`s are compared lexicographically. Thus `t1 < t2` is actually `&S::f < &S::f` and the compiler complains that there's not a binary `operator<` to compare these types. – 101010 Oct 22 '15 at 09:01
  • I suppose it makes sense that with parameter type of &S::f is deduced as void (S::* const)(). The address of the function (S::f in this case ) is a constant. You can try overloading global operator < for the type to get the behaviour you want. – rakeshdn Oct 22 '15 at 09:13
  • The goal was a class with mfptr as key in a map thing, but I think I will just skip that member as I have no better idea and I don't want to use hacks. Thanks all the comments! – Rathelor Oct 22 '15 at 09:26

1 Answers1

2

Tuples will try to do the comparison on a function pointer and you can only compare function pointers for equality. Please also refer to this question

Function pointers are not relationally comparable in C++. Equality comparisons are supported, except for situations when at least one of the pointers actually points to a virtual member function (in which case the result is unspecified).

Community
  • 1
  • 1
kittikun
  • 1,829
  • 1
  • 25
  • 33
  • if you look at the implementation of tuple_comparison.hpp, you will see that the operators (in your case lt) is iterating throught all the types inside the tuple and call < on them. So you will try to do a less comparison on function pointer, which is forbidden – kittikun Oct 22 '15 at 09:34