How can achieve external linkage for class static functions when the compiler thinks the code is local linkage or inline?
Consider three files:
traits.h:
template <typename T>
struct Traits
{
static int InlineFunction(T);
static int Function(T);
};
traitsimp.cpp:
#include "traits.h"
template <>
struct Traits<int>
{
static int InlineFunction(int) { return 42; }
static int Function(int);
};
int Traits<int>::Function(int i) { return i; }
main.cpp:
#include "traits.h"
int main()
{
int result = Traits<int>::Function(5);
result = Traits<int>::InlineFunction(result);
return 0;
}
When compiled receives:
$ g++ traitsimp.cpp main.cpp -o traitstest
/tmp/cc6taAop.o: In function `main':
main.cpp:(.text+0x1b): undefined reference to `Traits<int>::InlineFunction(int)'
collect2: ld returned 1 exit status
How do I convince the compiler to give InlineFunction external linkage while still writing the function within the class definition?