简体   繁体   English

模板类的模板类专业化

[英]Template class specialization with template class

Related questions: 相关问题:

Consider the following code: 请考虑以下代码:

  template <typename T>
  struct is_std_vector: std::false_type { };

  template<typename ValueType>
  struct is_std_vector<std::vector<ValueType>>: std::true_type { };

Why is such template class specialization syntax correct? 为什么这样的模板类专业化语法是正确的? The following seems more logical: 以下似乎更合乎逻辑:

  template <typename T>
  struct is_std_vector: std::false_type { };

  template<> //--- because it is is_std_vector specialization
  template<typename ValueType>
  struct is_std_vector<std::vector<ValueType>>: std::true_type { };

Class template partial specialization syntax closely mirrors function template syntax. 类模板部分特化语法紧密反映函数模板语法。 Indeed, the rules for ordering class template partial specializations is based on function template partial ordering. 实际上,排序类模板部分特化的规则基于函数模板部分排序。

The way you would write a function taking a vector<T> is: 使用vector<T>编写函数的方法是:

template <class T>
void is_std_vector(vector<T> ) { ... }

So the way you write a specialization on vector<T> is the same: 所以你在vector<T>上编写专门化的方式是一样的:

template <class T>
class is_std_vector<vector<T>> { ... };

Matching the specialization of is_std_vector would try to deduce T in vector<T> from some type argument A , so it makes a lot of sense that they're written the same way. 匹配的专业化is_std_vector会尝试推断Tvector<T>从某种类型的论证A ,所以它是一个很大的意义,他们正在写相同的方式。

For full specializations, we use template <> as placeholder signal to make full specializations look similar to partial specializations. 对于完全特化,我们使用template <>作为占位符信号,使完全特化看起来类似于部分特化。 I'm not sure what purpose an extra template <> would serve in this particular case. 我不确定在这种特殊情况下额外template <>用途是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM