1

I can't get the point why we should use explicit template instantiation. I thought it was used for exclitily specifying a family of classes/funtions/variables which would be used later in the program. So I tried the following example: main.cpp:

#include<iostream>
#include "test.h"

using std::cout;
using std::endl;

U<int> u;

int main()
{
        cout << u.a << endl;
}

test.h

template<class T>
class U
{
public:
        int a = 10;
};

Makefile:

TARGET := bin

$(TARGET): main.o
        g++ -g -o bin main.o
main.o: main.cpp
        g++ -g -std=c++1y -c main.cpp

clean:
        rm -rf *.o *.exe

And these program compiles, links and runs fine in spite of not using explicit instatiation. Couldn't you give an example where the explicit instatiation would be neccesary?

0 Answers0