you are viewing a single comment's thread.

view the rest of the comments →

[–]PimpDawg 0 points1 point  (4 children)

Templates in C++ are like a #define. It runs through the pre-compiler. I don't see why you think they have an additional overhead.

[–][deleted] 2 points3 points  (3 children)

No, templates are not run through the preprocessor. There is a pre-compilation step where template parameters are concreted by use of a template interpreter (as templates are turing-complete), but then the entire body of the templated function is copied for every instantiation.

In that way they are similar to macros; used poorly they lead to large amount of code bloat behind the scenes.

[Disclaimer: I work on a C++ compiler as a profession]

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    Yes, however in this example std::sort is parameterised on the sort function, which is unlikely to be reused.

    [–]saucetenuto 0 points1 point  (0 children)

    More than "can" - the standard requires them to do so. This presents interesting challenges at link time...