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

时间:2017-10-17 00:39:00

标签: c++ c++11 c++14 variadic-templates using-declaration

如何编译此代码?

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

1 个答案:

答案 0 :(得分:3)

带有参数包的

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

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