对变量参数进行特殊化类模板

问题描述 投票:0回答:1

我想为变量参数专门设计一个类模板。

template <typename... Ts>
struct TypeList
{
};

template <typename T>
class Foo
{
};

//Is next "specialization" even possible?
template <typename... Ts>
class Foo<TypeList<Ts...>>
{
};

Foo<TypeList<int, int>> i1{}; // OK
Foo<int, int> i2{}; // NOK:  error: wrong number of template arguments (2, should be 1)

我想让用户的 Foo 可以选择提供 TypeList 或显式类型列表。

c++ templates variadic-templates template-specialization
1个回答
2
投票

你可以做什么,允许两种语法。

template <typename ... Ts>
class Foo : Foo<TypeList<Ts...>>
{
};

// Specialization
template <typename ... Ts>
class Foo<TypeList<Ts...>>
{
// ...
};
© www.soinside.com 2019 - 2024. All rights reserved.