【发布时间】:2010-05-14 02:56:59
【问题描述】:
我有一个模板类 Matrix。我想为复杂类型专门化一个函数,其中 T 可以是任何东西。我试过这个:
6 template <typename T>
7 class Matrix {
8 public :
9 static void f();
10 };
11 template<typename T> void Matrix<T>::f() { cout << "generic" << endl; }
12 template<> void Matrix<double>::f() { cout << "double" << endl; }
13 template<typename T> void Matrix<std::complex<T> >::f() { cout << "complex" << endl; }
第 13 行无法编译。我该怎么做?
【问题讨论】:
-
我不明白这是怎么回事..
-
他正在尝试专门化非模板成员函数。那个问题也一样。
-
如果我的函数不是模板成员函数,第 11 行和第 12 行怎么编译得很好?
-
模板模板参数不是这个问题的答案吗?
标签: c++ templates function member