vendredi 22 juillet 2016

C++ - std::function as parameter for a functor

I wrote an Event class as a wrapper around callback functions, implemented as std::functions. This is what it looks like:

class Event
{
public:
    Event() : default_handler([]() {});
    Event(const std::function<void()> handler) : default_handler(handler);

    void SetHandler(std::function<void()> handler)
    {
        custom_handler = handler;
    }

    void operator()(void)
    {
        default_handler();
        custom_handler();
    }

private:
    const std::function<void()> default_handler;
    std::function<void()> custom_handler;
};

Then, inside another class, I have an instance of an Event:

class Control
{
public:
    Control();

//Should call constructor Event()
    Event myEvent1;
//Should call constructor Event(std::function<void()>)
    Event myEvent2([]() {/*do stuff... */})
};

This, however, won't compile on VC++, generating error C3646 (unknown override specifier) and error C4430 (missing type specifier - int assumed) for both handlers, with more syntax errors for myEvent2. Where did I go wrong?

Aucun commentaire:

Enregistrer un commentaire