【发布时间】:2019-05-20 13:33:16
【问题描述】:
我有这种情况:
#include <vector>
template<typename T>
T f() { return T(); }
template<>
template<typename T>
std::vector<T> f<std::vector<T>>() {
return { T() };
}
int main(){
f<std::vector<int>>();
}
我正在尝试为 std::vector<T> 专门化模板,但出现此错误:
error: too many template-parameter-lists
std::vector<T> f<std::vector<T>>() {
我怎样才能专攻std::vector<T>?
【问题讨论】:
-
不允许对模板函数进行部分特化。
-
这种情况我更喜欢标签调度:
template<>struct Tag{}; template<typename T> T f(tag<T>) {/*..*/} template<typename T> std::vector<T> f(tag<std::vector<T>>) {/*..*/}
标签: c++ templates template-specialization