11PerformanceTip25.1Although templates offer software-reusabilitybenefits, remember that multiple function-templatespecializations and class-template specializationsare instantiated in a program (at compile time),despite the fact that the template is written onlyonce. These copies can consume considerablememory.This is not normally an issue, though, because thecode generated by the template is the same size asthe code the programmer would have written toproduce the separate overloaded functions.2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 11 Performance Tip 25.1 Although templates offer software-reusability benefits, remember that multiple function-template specializations and class-template specializations are instantiated in a program (at compile time), despite the fact that the template is written only once. These copies can consume considerable memory. This is not normally an issue, though, because the code generated by the template is the same size as the code the programmer would have written to produce the separate overloaded functions
1225.3OverloadingFunctionTemplates: A function template may be overloaded- Other function templates that specifythe same name butdifferent parameters- Nontemplate functionsthat specify the same name butdifferent parameters-The compiler chooses the best function or specialization tomatch thefunction call.A nontemplatefunctionis chosenovera templatespecializationin case of a tie. Otherwise,multiple matches results in a compilation error(the compiler considers the function call to be an ambiguousfunctioncall)2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 12 25.3 Overloading Function Templates • A function template may be overloaded – Other function templates that specify the same name but different parameters – Nontemplate functions that specify the same name but different parameters – The compiler chooses the best function or specialization to match the function call • A nontemplate function is chosen over a template specialization in case of a tie • Otherwise, multiple matches results in a compilation error (the compiler considers the function call to be an ambiguous function call )