0

I'm trying to use Storing boost::function objects in a container approach #3, and gcc is complaining about this ->

EventComparator.hpp:39,34 - Error - invalid operands to binary expression ('const request_type' (aka 'const std::function') and 'const request_type')

template<typename Callback, typename Function>
bool Comparator(const Function & lhs, const Function & rhs) {
    typedef typename std::conditional < std::is_function<Callback>::value,
            typename std::add_pointer<Callback>::type, Callback >::type request_type;
    if (const request_type * lhs_internal = lhs.template target<request_type>()) {
        if (const request_type * rhs_internal = rhs.template target<request_type>())
            return *rhs_internal == *lhs_internal;
    }
    return false;
}

template<typename ...Args>
class EventComparator : std::function<Args...> {
public:
    typedef std::function<Args...> Function;

    bool (*typeHolder)(const Function &, const Function &);
public:
    EventComparator() {
    }

    template<typename T>
    EventComparator(T function)
        : Function(function), typeHolder(Comparator<T, Function>) {
    }

    template<typename T>
    EventComparator & operator=(T function) {
        Function::operator=(function);
        typeHolder = Comparator<T, Function>;
        return *this;
    }

    friend bool operator==(const Function & lhs, const EventComparator & rhs) {
        return rhs.typeHolder(lhs, rhs);
    }

    friend bool operator==(const EventComparator & lhs, const Function & rhs) {
        return rhs == lhs;
    }

    friend void swap(EventComparator & lhs, EventComparator & rhs) {
        lhs.swap(rhs);
        lhs.typeHolder.swap(rhs.typeHolder);
    }
};
Community
  • 1
  • 1
Agustin Alvarez
  • 339
  • 1
  • 2
  • 11
  • 1
    What is EventComparator supposed to do? All I can see it do is be copyable and swappable – sehe Aug 01 '13 at 19:16
  • Also `typedef std::function Function;` looks like an error, becuase it will be invalid unless Args is a unary pack containing only a function type. IOW: are you sure you shouldn't just be using Boost Signal2? It looks like you are out of your depth. – sehe Aug 01 '13 at 19:20
  • @sehe, EventComparator is a wrapper for std::function to allow std::function comparision. I'm using EventComparator to add and remove functions from a container. I found Boost Signal 2 slower than a simple delegate implementation :P, but i'm trying to use std::function. – Agustin Alvarez Aug 01 '13 at 19:21
  • Except, EventComparator doesn't model a calleable object, so it can't be a comparator; it doesn't compile (missing `template<>` clause) and your nested `Function` typedef seems to be broken – sehe Aug 01 '13 at 19:23
  • Huh. So Boost Signals2 is slow? I'd say that it working is a nice feature. Also, see libevent/libev (IIRC) – sehe Aug 01 '13 at 19:24
  • please avoid emotions in your question's title – Johannes Schaub - litb Aug 01 '13 at 19:24

0 Answers0