所以当使用模板显式实例化与完全模板类专门化时,我得到一个未定义的引用错误,但问题是,部分模板类专业化顺利进行而没有错误。
代码如下所示,有谁知道为什么?在这种情况下,完全专业化和部分专业化之间的区别是什么?
提前致谢。
// t.h
#include <iostream>
using namespace std;
template <typename T1, typename T2>
class A {
public:
void foo();
};
// t2.cpp
#include "t.h"
template<typename T1>
class A<T1, int> {
public:
void foo() {
cout << "T1, int" << endl;
}
};
template<>
class A<int, int> {
public:
void foo() {
cout << "int, int" << endl;
}
};
template class A<float, int>;
template class A<int, int>;
// t.cpp
#include "t.h"
int main() {
A<float, int> a;
a.foo(); // no error
A<int, int> a1;
a1.foo(); // undefined reference error, why?
return 0;
}
编译命令是g++ t.cpp t2.cpp -o t
和gcc 4.8.5。