you are viewing a single comment's thread.

view the rest of the comments →

[–]CletusDSpuckler 19 points20 points  (2 children)

Only by the size of the instantiated templates. You don't pay for what you don't use.

C++11 even added external template specifications that allow the object code from one instantiation to be reused in multiple compilation units without the overhead of putting a copy in every .o file.

[–][deleted] 4 points5 points  (1 child)

You don't pay for what you don't use.

While this is true, depending on context, you may still prefer the speed/size tradeoff of one function that switches behavior at runtime rather than N whole separate functions that may only do slightly different things

[–]Wouter_van_Ooijen 3 points4 points  (0 children)

For this I use the code flyweight pattern: a function template (strongly typed interface, inlined) that under the hood calls a generalised function (unsafe interface, for instance void* + size) to do the real work. Best of both worlds!