@fcarney said in template/macro for reducing redundant functions?:
Okay, I think I get it.
Partly. What a template is from the point of view of the binary is nothing - it does not produce anything. A template is way to tell your compiler to substitute template arguments at some point and produce a real living class (or function) which it then can compile to assembly. Instantiation happens on first use with a given set of template parameters ordinarily. In this case I "abuse" that peculiarity by saying: okay, there is a template function (the declaration) but when you need to substitute the types (i.e. instantiate the template on use) you can't, because you don't know the function body.
The extern template at the end is to make all that at all useful - fine there's no body, so the template can't be instantiated in the user code, but I can give you some instantiations I made explicitly somewhere else (in this case in the source). So it works like this:
The compiler sees there's a declaration. It also sees there are 2, 3, 4 instantiations that are done somewhere else (i.e. the declarations at the bottom). In the source it has the template body, and has been told to instantiate the template - that's fine, it can as it knows how.
When you use it somewhere it matches the stuff that's declared (i.e. the template declaration and the instantiations) against what you request. It puts a reference for the linker to resolve.
If there's an instantiation provided for the given set of template parameters, everything is fine - the linker's going to clean up the mess and link the function you have with the usage. If not, i.e. you used the template with a parameter that hand't been explicitly instantiated, then the linker complains - there's no such thing.