0

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?

cpplearner
  • 13,776
  • 2
  • 47
  • 72
Vahagn
  • 4,670
  • 9
  • 43
  • 72
  • [the explicit instantiation has no effect](http://eel.is/c++draft/temp.explicit#5) – cpplearner Apr 26 '17 at 15:46
  • Possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Henri Menke Apr 30 '17 at 21:54

0 Answers0