Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
like image 894
Nikola Smiljanić Avatar asked Jul 01 '26 20:07

Nikola Smiljanić


1 Answers

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.

like image 96
Justin Avatar answered Jul 03 '26 10:07

Justin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!