考虑以下在一个更大的项目中重现问题的最小示例:
规格.h:
#include <iostream>
class A
{
public:
template<typename T>
T test(const std::string& a)
{
std::cout << "DEFAULT CALLED WITH " << a << "\n";
return T();
}
};
其他.cpp:
#include "spec.h"
template<>
float A::test<float>(const std::string& a)
{
std::cout << "SPECIAL CALLED WITH " << a << "\n";
return float();
}
规范.cpp:
#include <iostream>
#include "spec.h"
int main()
{
A a;
a.test<int>("int");
a.test<float>("float");
return 0;
}
汇编:
$ make
rm -f *.o lib.a output
clang++ -g other.cpp -c
clang++ -g spec.cpp -c
ar cr lib.a other.o
clang++ -g -o output lib.a spec.o
rm -f *.o output2
clang++ -g other.cpp -c
clang++ -g spec.cpp -c
clang++ -g -o output2 other.o spec.o
$ ./output
DEFAULT CALLED WITH int
DEFAULT CALLED WITH float
$ ./output2
DEFAULT CALLED WITH int
SPECIAL CALLED WITH float
问题:
为什么会这样?它会以某种方式被剥夺吗?lib.a 和直接使用目标文件有什么区别?:-)
谢谢!