Search code examples
c++c++11c++14variadic-templatesusing-declaration

Using declaration contains unexpanded parameter pack


How do I get this code to compile?

struct type1 {};
struct type2 {};

struct handler1
{
    void handle(type1){}
};

struct handler2
{
    void handle(type2){}
};

template <typename... Handlers>
struct TheHandler : Handlers...
{
    using Handlers::handle...; // DOESN'T COMPILE
};

TheHandler<handler1, handler2> handler;
handler.handle(type1());

Solution

  • using with parameter packs was added in C++17, so your code would just work in C++17.

    As a workaround for C++14, you can use recursion. The proposal for using... shows how to do this:

    template <typename Handler0, typename... Handlers>
    struct TheHandler : Handler0, TheHandler<Handlers...>
    {
        using Handler0::handle;
        using TheHandler<Handlers...>::handle;
    };
    
    template <typename Handler>
    struct TheHandler<Handler> : Handler
    {
        using Handler::handle;
    };
    

    On Godbolt

    It is possible to achieve logarithmic recursion depth, if you wish to do so.