1

How can we make a variadic callback in not template class without making class template ?

    class Foo{
    ...
    template <typename ...Args>
    using Callback = std::function<void(Args...)>;

    template <typename ...Args>
    void registerCallback(Callback<Args...> cb)
    {
        mCallbacks.emplace_back(cb);
    }

private:
    std::vector<Callback<...>> mCallbacks;
};
Grayowl
  • 77
  • 3
  • 12
  • Maybe [this](https://stackoverflow.com/questions/31919895/stdfunction-variable-arguments-in-one-vector-map) helps? – Max Oct 19 '18 at 12:39
  • Why don't you make another variadic class for callbacks and make it a member of this class? – The Quantum Physicist Oct 19 '18 at 12:42
  • 3
    You can't store different `Callback<...>` in a vector so I hate to say it but this might be one of those cases where you need a `std::function` and make it more of a C interface. – NathanOliver Oct 19 '18 at 12:43
  • How do you expect to call your callback ? `int i = /*..*/; mCallbacks[i](/*Which arguments are valid for i ?*/)`. or even worst, `for (auto f : mCallbacks) { f(/*What to put here ? */);}` – Jarod42 Oct 19 '18 at 14:18
  • Thanks. I will use this => You can't store different Callback<...> in a vector so I hate to say it but this might be one of those cases where you need a std::function and make it more of a C interface – Grayowl Oct 20 '18 at 18:58

0 Answers0