I can understand how it work if they are inlined. But if they are not, how does it work? does all object files get their own copy of for example the function template anyway
It depends. Some of the more popular implementations do generate a copy of the instantiation code in each object file which triggers an instantiation, and count on the linker to throw out all but one. Other compilers use some sort of repository, where instantiations are stored; if the instantiation is already present, the compiler doesn't bother regenerating it. This solution is significantly faster and uses less disk than the first solution, but it's also a lot harder to get right. (The compiler has to generate a new instantiation not only if one isn't present, but if any of the files the instantiation depends on have changed.)