0

I was learning about function templates and the tutorial said:

"Once a template is defined, the compiler can use the template to generate as many overloaded functions (or classes) as needed, each using different actual types!".

Is this true? Does that mean that a function template is just the compiler generating overloaded functions for different types? Does the compiler scan through your code checking if you made any user-defined types and then generates overloaded functions for those user-defined types?

Is that all there is to function templates or does the compiler do additional things?

  • 1
    A new instance is generated when a template is used. The compiler doesn't instantiate the template for every type ever mentioned in the program, but does for every type (or rather, every set of template arguments, which may not all be types) the template is actually used with. – Igor Tandetnik Dec 18 '22 at 16:04
  • In the past I am talking 1995 here, when I first used templates. C++ had a precompiler that compiled templates in a precompilation step to C++ code (actual files) which then where compiled with the (non-template supporting) C++ compiler. Since then that process has been optimized but still it is a useful mental model. – Pepijn Kramer Dec 18 '22 at 16:08
  • Yes, that means that a function template is "just" the compiler generating overloaded functions for different types (if we disregard non-type parameters). It is related to [parametric polymorphism](https://en.wikipedia.org/wiki/Parametric_polymorphism), and the roots can be found in the [CLU](https://en.wikipedia.org/wiki/CLU_(programming_language)) language. – molbdnilo Dec 18 '22 at 16:14
  • Not sure you've got the right idea, if you're referring to user-defined types here. For a given set of template arguments the compiler generates the function, but it doesn't make a difference whether type template arguments are classes you created yourself, classes you included from the standard library or even primitive types like `int`... – fabian Dec 18 '22 at 16:15
  • It might be helpful if we think we are the compiler. What would we do? We verify some basic things in syntax, looking at the function template itself. Following that, only when we see a potentially template function call, we look up the function template, and would try to instantiate it using the explicit template parameters and/or actual function parameters. Once we figure out all the types for the instantiation, we write an actual working function code, filling out the types in the template...unless we already did write the same thing. – Stephen Dec 18 '22 at 16:18
  • A template is just like a cookie cutter when baking x-mas cookies at home. It can be used to stamp out many different cookies (functions) from different dough (types). You don't ever *need* templates - you could always write out all the different function overloads by hand, it would just be a lot of work - templates offer you a shortcut. – Jesper Juhl Dec 18 '22 at 16:20

0 Answers0