使用声明包含未扩展的参数包

Using declaration contains unexpanded parameter pack

如何编译此代码?

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());

using 在 C++17 中添加了参数包,因此您的代码 would just work in C++17.

作为 C++14 的解决方法,您可以使用递归。 The proposal for using... 展示了如何做到这一点:

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

如果您愿意,可以实现对数递归深度。