mercredi 26 avril 2017

Will extern template reduce the compile time for a full specialization?

Consider a program consists of files AA.hpp, A1.hpp, A1.cpp, and main.cpp compiled with g++ -std=c++11 main.cpp A1.cpp.


// file AA.hpp
template < int > struct AA;


// file A1.hpp
#include "AA.hpp"
template <> struct AA<1> { /*implementation goes here...*/ };
extern template struct AA<1>;


// file A1.cpp
#include "A1.hpp"
template struct AA<1>;


// file main.cpp
#include "A1.hpp"
int main()
{
    AA<1> a1;
    // use a1 ...
    return 0;
}


Compare this with a scenario when there is no A1.cpp and there is no explicit instantiation declaration in A1.hpp. Will I get any compilation time benefits in the first scenario? And why?

Another question. What if I make a shared library of A1.cpp by g++ -std=c++11 -shared -o libA1.so A1.cpp and then make executable with g++ -std=c++11 -lA1 main.cpp? Will the calls of A1's functions in man.cpp refer to the code in libA1.so or they will be generated (inlined or not) in the executable?

Aucun commentaire:

Enregistrer un commentaire