When using Templates in C++ how does the compiler know how many data-types to instantiate?

109 views Asked by At

I just started learning Templates. How does the compiler know which data-types are required? Can someone please explain how compilers treat and work with templates?

1

There are 1 answers

2
Logicrat On

It's based on actual instantiation. If you actually declare on object using one of your templates (and any given set of parameters to it), then code will be generated. A decent compiler will generate code for exactly those configurations that you use, and none for those you do not use. I hope that explains it adequately.