Adriaan
Adriaan

Reputation: 51

Code reuse (or not) in C++ templates

In all books I've read until now, they said that C++ templates generate one instance of code for each type we use.

On the other and, the books said that in C# the code is reused.

So I made a search in many books and I found in one very old book the following examples for C#.

1) Value types

List<int> intList1  = new List<int>();
List<int> intList2  = new List<int>();
List<bool> boolList = new List<bool>();

In this case (value types) the compiler generates ONE instance of code for both intList1 and intList2 (same type) and ONE instance of code for boolList.

2) Reference types

List<Dog> dogList1 = new List<Dog>();
List<Dog> dogList2 = new List<Dog>();
List<Cat> catList  = new List<Cat>();

In this case (reference types) the compiler generates JUST ONLY ONE instance of code for dogList1, dogList1 and catList because as long as it can represent the pointer to the object, all reference types can share a single implementation.

This is very clear to me (as a C++ dev) but I'm courious about the same thing concerning c++ templates.

Long before C # was created, C++ had value types and reference types too.

So lets rewrite the avove examples in C++

1) Value types

vector<int> intList1;
vector<int> intList2;
vector<bool> boolList;

In this case I have no doubt that intList1 and intList2 share the same code and boolList needs another implementation

2) Reference types

vector<Dog *> dogList1;
vector<Dog *> dogList2;
vector<Cat *> catList;

My question is:

Can I conclude that in the second case (vectors of pointers) all share the same template instantiation as long as are all vector pointers ( vector<Dog *> and vector<Cat *> ) and all pointers are of the same size (32 or 64 bit depending on the platform)?

Upvotes: 5

Views: 737

Answers (2)

alexeykuzmin0
alexeykuzmin0

Reputation: 6440

Probably, you can use static variables to check this, something like this:

#include <iostream>
using namespace std;

template <typename T>
class Wrapper
{
public:
    static int count;
    T d_wrapped;

    Wrapper(T i_wrap) : d_wrapped(i_wrap)
    {
        ++count;
    }
};

template <typename T>
int Wrapper<T>::count = 0;

int main() {
    Wrapper<int> wi(1);
    Wrapper<int> wi2(2);
    Wrapper<float> wf(1.0f);

    Wrapper<int*> wip(new int());
    Wrapper<float*> wfp(new float());

    cout << Wrapper<int>::count << ' ' << Wrapper<float>::count << '\n' <<
            Wrapper<int*>::count << ' ' << Wrapper<float*>::count << '\n';
    return 0;
}

Ideone live code

Launch on ideone shows that even for pointers we have separate static variables.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234715

(There's a fair bit of confusion in your question, but let me skip over that and address the actual question).

No you cannot assume that.

This is chiefly because pointers do not necessarily have to be the same size, irrespective of whether your system is 32 or 64 bit.

Upvotes: 4

Related Questions