【发布时间】:2017-01-05 11:46:40
【问题描述】:
相关问题:
- c++ nested template specialization with template class
- template class specialization with template class parameter
考虑以下代码:
template <typename T>
struct is_std_vector: std::false_type { };
template<typename ValueType>
struct is_std_vector<std::vector<ValueType>>: std::true_type { };
为什么这样的模板类特化语法是正确的? 以下似乎更合乎逻辑:
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 { };
【问题讨论】:
-
第二个完全不合逻辑。
template<>意味着您明确地只专注于一种类型的T。您正在做的是部分专业化(您指定了 T 的无限子集,即仅满足std::vector<some_other_t>形式的子集)
标签: c++ templates template-specialization