【问题标题】:Specialize class template for variadic parameters专门用于可变参数的类模板
【发布时间】:2020-05-13 09:05:19
【问题描述】:

我想专门为可变参数创建一个类模板:

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】:

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

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

    【讨论】:

    • "Specialize class template for variadic parameters",所以其实是相反的顺序,专门为TypeList
    • 太好了,谢谢!是否也可以为默认类型列表提供专门化,例如 template class MsgDispatcher { }
    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多