Monday, September 17, 2012

how template works

template

This section is about how a template works. Remember when I said that the alternative to copying and pasting lots of code was to use a template? Well, that’s exactly how C++ implements a template. C++ goes through the template definition, copies the code, and replaces every instance of the parameterized type name with the actual type name. Figure shows how C++ basically takes one copy of the code and converts it into as many copies as are needed. 
how template works


The beauty of template implementation is that instead of managing all the copying and pasting yourself, you make the compiler do it instead, allowing you to maintain only one copy of the code. 


No comments:

Post a Comment