i dont understand how templates can cause code bloat in a binary file...does anyone happen to have an easy to understand explanation of this?

#include <iostream>

template <typename T>
void display(T value)
{
    std::cout<< value <<'\n';
}

int main()
{
    int a = 123;
    double b = 123.456;

    display(a);
    display(b);
}

This causes the template to be instantiated twice, once with each unique signature:

#include <iostream>

void display(int value)
{
    std::cout<< value <<'\n';
}

void display(double value)
{
    std::cout<< value <<'\n';
}

int main()
{
    int a = 123;
    double b = 123.456;

    display(a);
    display(b);
}

The first instantiation of a template with different parameters will generate code specified by the template using those parameters. The binary will grow correspondingly with each new unique instantiation (subsequent instantiations of with the same parameters will not generate more code).

Edited 5 Years Ago by Narue: n/a

nice examples....but i fail to understand your paragraph :/ maybe u can dumb it down for me lol

Let's say you build houses. You use a blueprint (a template) to build those houses. Each time a new person(template argument) asks for a house, you build a new one. The houses are the same because they're built from the same blueprint, but different people live in each one.

nice examples....but i fail to understand your paragraph :/ maybe u can dumb it down for me lol

Templates don't support implicit conversions so they'll create a new functions/objects for each type, hence code bloat.

Edited 5 Years Ago by gerard4143: n/a

Templates are designed to generate code at compile time. Due to this design, the only way they will ever contribute to the ultimate size of a binary is by making it larger. Think of templates as incomplete sections of code that get filled in at compile time. So if you have

template <typename T> class Foo { T t; }

that is a way for the compiler to know how to generate code based on the types used in place of T. For instance,

Foo<int> fi;
Foo<char> fc;
Foo<double> fd;

Would result (basically) in

class Foo { int t; }
class Foo { char t; }
class Foo { double t; }

Which, on the surface, doesnt look like much but remember that entire headers are written as templated code (standard template library) so this gets very large very quick.

There is another thing that is not always obvious. When you use templates to do meta-programming you can get serious expansion in your resulting binary. Consider

template <int N> int fact () { return fact<N-1>() * N; }
template <> int fact<1> () { return 1; }

Which calculates a factorial at compile time. You might use it like std::cout << fact<6>() << std::endl; . The problem is in the code that is generated. fact<6> generates a code listing of 9K bytes while fact<20> generates a code listing of 21K bytes. Realize that this is assembly listing which will likely get reduced at the final translation stage but helps gets the point across, I think.

This article has been dead for over six months. Start a new discussion instead.