Take the following example:
// A.h
class A
{
public:
int v = 2;
template <typename T>
int f(T t);
};
// A.cpp
#include "A.h"
template <typename T>
int A::f(T t)
{
return v + t;
}
template <>
int A::f<int>(int t);
// main.cpp
#include <stdio.h>
#include "A.h"
int main()
{
A a;
printf("%d\n", a.f(3));
return 0;
}
When building this with clang -std=c++14
(or g++), I get the following error:
main.cpp:8: undefined reference to `int A::f<int>(int)'
Indeed, nm A.o
doesn't show any symbols. Why didn't the explicit instantiation of A::f<int>
inside A.cpp
actually instantiate the function?