c++ - Does using templates saves memory -
in understanding templates allow write 1 function or class works different data types.
containers such stack or linked lists used store data can store 1 type of data @ time. store store different types of data required write different versions of same container class. can save repetition of code writing class template.
i know saves writing effort on programmer's part. want know using templates saves memory or not.
it depends of template write. first of all, if template create instance on every data type waste memory. simply, code generated instantiation of template occupy program space each instance. having such template same writing each instance hand.
on other hand template can optimized lot if code of each instance of given data type same. think container template vector stores pointers kind of types. why should need code store pointers int instead of pointers float? take @ stl implementation vector , specialisation pointer types. instances of vector template pointers derives one! base class implements needed functionality pointers. uses typically void pointers that.
the code instance given data type create cast concrete pointer type. cast not generate processor executable code done without program space cost.
and there huge amount of templates operations during compile time. called meta template programming. templates generates no code @ , comes without cost program space.
in short: have remember 3 types of template code answer.
Comments
Post a Comment