【问题标题】:Template template partial specialization failure: "expected a class template"模板模板部分特化失败:“期望一个类模板”
【发布时间】:2016-07-25 08:14:54
【问题描述】:

此示例代码生成expected a class template, got std::pair <_T1, _T2>。我尝试使用struct Struct <std::pair> {};,但随后参数T 和M 变得无法推断。如何避免这种情况?

    template <template <class...> class>
    struct Struct {};

    template <class T, class M>
    struct Struct <std::pair <T, M>> {};

【问题讨论】:

  • std::pair 是类模板,std::pair &lt;_T1, _T2&gt; 不是。您可以将主模板声明更改为template &lt;class&gt; struct Struct {};。

标签: c++ templates partial-specialization


【解决方案1】:

取决于你想要什么

template <template <class...> class>
struct Struct {};

template <>
struct Struct <std::pair>
{
    // Specialization
};

或

template <typename> struct Struct {};

template <typename First, typename Second>
struct Struct <std::pair<First, Second>>
{
    // Specialization
};

【讨论】:

    【解决方案2】:

    这不是您的模板的有效特化。

    原因是因为std::pair&lt;T, M&gt; 是类模板std::pair 的完全特化,因此是一个类。您的模板需要一个类模板参数,这正是编译器告诉您的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      相关资源
      最近更新 更多