I'm running into an issue with template specialization. The following builds and runs, but I'm not sure it should:
// template.h
template <typename T>
class interface{
public:
void doSomething();
};
// template.cpp
#include "template.h"
#include <iostream>
template <> void interface<int>::doSomething(){std::cout << "Did something\n";}
// main.cpp
#include "template.h"
int main(){
interface<int> instance;
instance.doSomething();
return 0;
};
g++ main.cpp template.cpp; ./a.out
My understanding was that all compilation units were supposed to have access to all template specializations, which main.cpp does not. If I understand what's happening correctly, the specialization in template.cpp also instantiates interface< int >, so the symbol is generated and everything links happily. However, I'm pretty sure this falls into the realm of undefinedness and I can't count on this always working everywhere. Can someone please confirm or deny my suspicions?
Thanks!