4

In order to support generic programming, C++ has templates. But since templates are purely a compile-time construct (much like anything else in C++), it looks like there's not much point in trying to use them in a dll.

Let's say we want to create a math library that comprises a number of templated functions. As long as this library is statically linked with an application, we're OK. You can argue that we can statically link a dll to an application too (provided you have access to the source/or object files) but most of the time we just want to use the dll (by loading it via LoadLibrary and calling a particular function with GetProcAddress)

So, does the above mean that we can't really use templates in a dll (if they're to be used externally)? And what are (if any) alternatives, apart from static libraries?

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
dsp_user
  • 2,061
  • 2
  • 16
  • 23
  • 4
    Header only libraries come to mind. Most of `boost` is header only as a matter of fact. – StoryTeller - Unslander Monica Jan 03 '18 at 13:19
  • 2
    You can make common specializations of those templates to be imported from the dll to save size of binaries if this library is supposed to be used in many executables. – user7860670 Jan 03 '18 at 13:22
  • 1
    There's also the option of making the template itself a "thin" wrapper around code that is completely type unaware. Essentially, the template generates the boiler-plate on the client side, while the DLL contains the stuff that do heavy lifting. – StoryTeller - Unslander Monica Jan 03 '18 at 13:25
  • The more I think about this, the more it doesn't make sense. The code for the templates will only be generated if there's code calling it, right? – dsp_user Jan 03 '18 at 13:37
  • 1
    @VTT, You' re right about the template specializations, though that's not really generic. – dsp_user Jan 03 '18 at 14:09
  • I didn't mean to ask this as a trick question or anything. I'm just interested in alternatives (e.g header libraries as suggested in a comment). – dsp_user Jan 03 '18 at 14:12
  • @dsp_user Not clear what you are asking. You can put you code in DLL, but templates must be in header file. `GetProcAddress` is hard to use with C++ code as you will have to take mangling in account. And why do you need templates if you want dynamically load DLL? –  Jan 03 '18 at 15:16
  • *"So, does the above mean that we can't really use templates in a dll (if they're to be used externally)?"* - Yes. *"And what are (if any) alternatives, apart from static libraries?"* - Don't use a template in the DLL interface. – Christian Hackl Jan 03 '18 at 17:20
  • @Christian Hackl Ok, I can't object to what you wrote there. – dsp_user Jan 03 '18 at 20:30
  • @dsp_user, really sad this question hasn't had much attention. I think a thorough answer to this would really have the chance to be a didactic reference to whoever wants to know when you should used template and when you should not, and what the criteria are. – Enlico Feb 01 '23 at 15:18
  • 1
    @dsp_user, however, [this](https://stackoverflow.com/a/16493574/5825294) is a very good answer. – Enlico Feb 01 '23 at 15:42
  • @Enlico, yes, that's an interesting discussion and that particular answer by Ben is indeed very good. Unfortunately, templates in c++ are not as useful as in some other languages because they need to be recompiled for every new type that you want your template to support. Thanks for revisiting this thread after more than 5 years. – dsp_user Feb 01 '23 at 18:41

0 Answers0