-1

I had a question while studying a template

  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.

  2. Compilation takes longer because most of the code is generated during compilation.

I think these two wordings do not work together, so my conclusion is that linking happens before the template is instantiated at compile time. What do you think?

Holt
  • 36,600
  • 7
  • 92
  • 139
김정연
  • 47
  • 2
  • 1
    "So my conclusion is that linking happens before the template is instantiated at compile time." - What? That doesn't make *any* sense. Linking happens *after* the compiler has generated the object files that the linker links. How could linking possibly happen before compilation?? Whatever your conclusion is, it is *wrong*. – Jesper Juhl Nov 03 '19 at 15:09
  • 1
    Does https://stackoverflow.com/questions/5094908 answer your question? – AProgrammer Nov 03 '19 at 15:13
  • You should explain the root of your issues with template and how you came to the above conclusion, otherwise it will be hard to explain why such conclusion is incorrect. – Holt Nov 03 '19 at 15:35
  • (1) _"...linker will not find..."_ the linker will not be __looking__ for the code as it has not been used. (2) Templates generate code when needed (sort of); if you are not using pre-crafted templates you have to write the code anyway - compilation time is therefore unchanged. – Richard Critten Nov 03 '19 at 16:25
  • It depends on the implementation (comnpiler and linker). There are techniques to explicitly instantiate templates at compile time (e.g. one compilation unit uses the templates without instantiating, another instantiates the template without using it). There are smart linkers, which can cooperate with the compiler, so template instantiation is done by the linker (based on information emitted by the compiler). Theoretically, template instantiation can also happen at run time (e.g. an implementation that targets a virtual machine) but I've yet to encounter an implementation which does that. – Peter Nov 03 '19 at 18:25

1 Answers1

0

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.

  1. 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.

  • If linking occurs after compilation, template instantiation occurs during compilation, so the code for the template should be included in the obj file and linking should be possible? – 김정연 Nov 05 '19 at 01:57
  • @김정연 Yeah, I recon it would. –  Nov 05 '19 at 04:27