gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:12:37 26 4
gpt4 key购买 nike

我如何编译这段代码?

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 with parameter packs 是在 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

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

关于c++ - 使用声明包含未扩展的参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46780842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com