In the related question, Why can templates only be implemented in the header file?, two strategies are described for avoiding linker errors:
- Implement the entire template in the header file; OR
- Explicitly instantiate templates using the
template class Foo<int>
syntax.
From what I gather, #1 seems to be the "preferred" method and is used in every library - presumably because it is difficult or impossible to predict which types library consumers will want to use. My question concerns strategy #2.
Specifically, why would I ever want to do this?
FYI: I'm mostly new to C++ with a heavy C# background. I'm accustomed to the .NET style of generics which is a little more straightforward, and yes, I understand why they are different. However, I assume that C++ developers still care about coupling. Does form #2 not introduce a particularly annoying form of coupling, wherein developers have to constantly edit the template's .cpp file just to use the template with a different, previously-unforeseen type? Doesn't that kind of defeat the purpose of templates?
Are there legitimate or important use cases associated with solution #2?