The simple answer is not to use std::function<...> in this case, but something like std::function<...> , which defines equality operators. The approach to defining an equality operator for function<...> is to determine, when building, whether the actual functional object really contains the equality operator and, if so, make the object comparable. Otherwise, you would either make a mistake or consider objects containing this type of object of a particular function to be incomparable.
However, direct observation is that most functional objects are not comparable! For example, lambda functions are not comparable, and std::bind() and std::mem_fn() also do not give comparable function objects. Again, a custom implementation may exist for std::bind() and std::mem_fn() . It is impossible to make lambda functions comparable if they do not have an empty capture, in which case they could be turned into pointers to functions and they could be compared.
The implementation of equality function objects is too long to quickly enter an answer. However, you can take a look at my github implementation for comparable comparisons of bind() and mem_fn() . See this answer for a comparable version of the equality std::function<...> . Perhaps it is desirable that the lambda functions are comparable if they have the same signature and all the fixed values ββare equal to each other.
All of the above, if you can avoid the need, is probably best avoided. However, I came across some use cases where a comparable std::function<...> would be quite convenient, despite its limitations (i.e. not all function objects will be covered).
source share