You can see if clang respects the external binding for explicit template instances. This may apply to non-templates, but otherwise you could “make” work for templates.
Simple synopsis:
lib1.h
template <typename T=int> struct ATemplate { T f() { return 123; } };
add file lib1_instantiate.cpp
#include "lib1.h" template struct ATemplate<int>; template struct ATemplate<unsigned int>; template struct ATemplate<long>; // etc.
This should create instances of named templates with external binding.
If you are stuck with a non-template class and the trick above does not work for this, you can wrap it like this:
instantiate.cpp:
namespace hidden_details { template <class libtype> struct instantiator : public libtype
If you're out of luck, you will have to “use” the built-in members to get an external connection. A common trick is to use the address of these participants (you will not need to implement delegation):
instantiate.cpp:
static void force_use_A() { void* unused = (void*) &A::f; }
but
- converting to (void *) causes undefined behavior (you cannot compile this with -pedantic -Werror on gcc)
- for overloads you will need to specify ugly throws to eliminate them.
NTN
sehe
source share