1.Templates do not instantiate code if they have not been used yet, so the compiler will not implement the template in the obj file and the linker will not find the corresponding contents in the obj file.
Templates are basically blueprints of how to generate a class at compile time. Thus, the compiler can look at a template and generate a class based off of it at compile time. However, what this is saying that that the compiler doesn't bother generating a specific class from the template until it knows you need to use one.
- Compilation takes longer because most of the code is generated during compilation.
Yeah. I mean it depends on a lot of things, but because it is generated at compile time, any template would, in theory at least, lengthen compile time.
I think these two wordings do not work together...
Not sure what seems contradictory here. 2 says the compiler does the heavy lifting for creating a specific templated class. 1 says that it doesn't bother doing so unless it needs to. Both of these can be true.
...so my conclusion is that linking happens before the template is instantiated at compile time. What do you think?
Nope. Can't happen. Compiling always happens before linking. Wish I had a more in-depth explanation, but that's kind of it. That's just not how C++ works.
To clear up confusion, I think what you might be missing is that 1 is saying is that if you don't use the template, the linker won't be able to find it in the object file because the compiler will never generate the specific class in the first place.