Quantcast
Channel: Nikolay Igotti
Viewing all articles
Browse latest Browse all 30

Explicit template instantiation in shared libraries

$
0
0
Now a little bit of C++ stuff. Usually I consider this language a bit undercooked, in its "advanced" features, like templates, but sometimes they could be useful. Consider following simple example:
a.hpp:
class C {public:template<class T> void run(T x);
};
a.cpp:
#include"a.hpp"
#include<stdio.h>template<class T> void C::run(T x) {if (x > 0) {
    printf("positive\n");
  }
}

// Note this!templatevoid C::run<int>(int x);
b.cpp:
#include"a.hpp"voidfoo() {
  C().run(3);
}
If we link those files with g++ -fPIC -shared a.cpp b.cpp -o liba.so unless explicit template instantiation line included, this library will fail to load, due to unresolved symbols.

Viewing all articles
Browse latest Browse all 30

Trending Articles