I'm using explicit template instantiation. As far as I understand, because I'm instantiating in the header file, the following example should violate the one definition rule. But not a single compiler I've tried has a problem with it. What am I missing? Why does this work? And is it guaranteed to work on every compiler?
A.h:
#ifndef A_H
#define A_H
template<class T>
struct A
{
T x;
};
// instantiate
template struct A<int>;
template struct A<float>;
template struct A<double>;
#endif
file1.cpp:
#include "A.h"
A<int> f();
int main()
{
A<int> a = f();
}
file2.cpp:
#include "A.h"
A<int> f()
{
return A<int>();
}