C++ overload template with specific signature

Viewed 305

I have the following:

struct Args;

template <typename T>
void DoStuff(T&& callback) {
    // ... do stuff
    MyArgs args = ...
    callback(args);
}

Which is great, I can do the following:

void callback(const Args &);
DoStuff(callback);

And:

DoStuff([](const Args &) { ... });

And:

class CallbackClass {
    operator()(const Args &);
};
CallbackClass myCallbackClass;
DoStuff(myCallbackClass);

All is nice and good, now I want to do two improvements here:

  1. Enforce that the signature of T is always void(const Args &) because in the current state of things I can do the following:

    void badCallback(Args);
    DoStuff(badCallback);
    
  2. Allow (in addition to the previous options) to pass an object with an specific named member function that would also be allowed to be a callback, example:

    class NamedCallback {
        void PerformCallback(const Args &);
    };
    NamedCallback myNamedCallback;
    DoStuff(myNamedCallback);
    

Is it possible?

2 Answers
Related