gpt4 book ai didi

c++ - 从另一个模板引用可变模板参数包

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:58 24 4
gpt4 key购买 nike

我有一个定义类型集合的可变参数模板类

template <typename ... Types> class TypePack { };

多次实例化

typedef TypePack<T1, T2, T3> Pack1;
typedef TypePack<T1, T2, T4> Pack2;

我想从其他模板中引用TypePack参数

template <typename Pack> Client {
static constexpr std::array<Foo, sizeof...(Pack::Types)> foos {
make_foo<Pack::Types>()...
};
};
typedef Client<Pack1> Client1;
typedef Client<Pack2> Client2;

上面的代码显然是错误的,无法编译。它只是说明我想要实现的目标。

我可以通过宏定义 Pack1Pack2,但我感觉应该可以在 C++14 中使用可变参数模板来实现

最佳答案

您正在寻找的是部分特化:

template <typename Pack> struct Client;

template <class... Ts>
struct Client<TypePack<Ts...>>
{
static constexpr std::array<Foo, sizeof...(Ts)> foos {{
make_foo<Ts>()...
}};
};

// don't forget the definition
template <class... Ts>
constexpr std::array<Foo, sizeof...(Ts)> Client<TypePack<Ts...>>::foos;

关于c++ - 从另一个模板引用可变模板参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42374759/

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