はるかに大きなプロジェクトで問題を再現する次の最小限の例を検討してください。
スペック.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();
}
spec.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 と直接オブジェクト ファイルの使用の違いは何ですか? :-)
ありがとう!