Do Templates Increase Executable Size ?

Do Templates Increase Executable Size ?

Since template code is generated at compile time, it implies that there should be a separate set of code for each template objects created. Does that mean templates increase the executable size?

Not necessarily. Whether templates increase the program’s size depends on how you’re actually using them in your code and the compiler’s cleverness. It is true that injudicious usage of templates can cause a problem known as “code bloat”. For example, one of the following instances of the vector template is redundant; on a 32-bit platform, both of them result in two copies of identical code:

vector < int > vi; vector < long > vl;

Worse yet, because pointer types are distinct, excessive instantiation of pointer-based templates also creates redundant copies of identical code:

// Shape is base of Rectangle and Squarevector < Shape* > vs; vector < Rectangle* > vr;vector < Square* > vsq;

In this case, three distinct copies of vector are instantiated, one for each pointer type. You can easily avoid this redundancy by using polymorphism and a vector of a pointer to the base class, i.e., vector < Shape* >:

Rectangle rec;vs.pushback (&rec); // pointer to derived// calls Rectangle::Draw if vs[0] is of type Rectangle*vs[0]->Draw();

In fact, sophisticated use of templates can actually reduce the program’s size rather than increase it. This is because the compiler must generate code on a selective basis: it generates code only for member functions that are actually used in the program, whereas member functions that aren’t referenced in the program aren’t generated. Thus, if vector has 25 member functions (including constructors, assignment operator, and destructor). However, if a program only uses the push_back(), operator [], default constructor and destructor of vector < Shape* >. In this case, the compiler doesn’t generate code for the rest of the member functions. Consequently, program’s size is reduced. By contrast, if you’d chosen to implement vector as an ordinary class (i.e., not a class template), the compiler would have generated code for all the member functions of that class.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as