samedi 9 septembre 2017

How can i use C++11 explicit templates to reduce binary size?

I am new to the concept of explicit templates, and I'm having troubles in using it and seeing any difference in the binary size. The simple code i wrote goes like this:

comp.cc:

  #include "comp.h"

    using namespace std;

    template <typename T>
    int compare(const T& x, const T& y)
    {
        return x < y ? 0 : 1;
    }


    int func()
    {
        compare(10,9);
        compare(1.5, 7.8);
    }

comp.h:

template <typename T>
int compare(const T& x, const T& y);

main.cc:

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

using namespace std;

extern template int compare(const int&, const int&);
extern template int compare(const double&, const double&);

int main()
{
    compare(10,9);
    cout << compare(1.5, 7.8) << endl;
    return 0;
}

However, not matter if I compile the code with or without the explicit definitions, the size of the binary remains the same. I guess I am missing something simple, but could not find what it was.

Aucun commentaire:

Enregistrer un commentaire