Here's some simplified code to demonstrate the problem I have.
I have a template function for which I only wish to compile certain fixed instantiations.
The function declarations are:
// *** template.h ***
int square (int x);
double square (double x);
The definitions are:
// *** template.cpp ***
#include "template.h"
// (template definition unusually in a code rather than header file)
template <typename T>
T square (T x)
{
return x*x;
}
// explicit instantiations
template int square (int x);
template float square (float x);
And, an example use is:
// *** main.cpp ***
#include <iostream>
using namespace std;
#include "template.h"
int main (void)
{
cout << square(2) << endl;
cout << square(2.5) << endl;
}
An attempt to compile this results in a link errors, roughly:
main.obj : unresolved external symbol "int square(int)" referenced in function main
I understand what the problem is: the function signatures of my explicit template instantiations do not match those in the header file.
What is the syntax for the (forward) declaration of the explicit template instantiations please? I do not wish to forward declare the template definition, or to move the template definition into a header file.
For what it's worth, I do have a workaround, which is to use wrapper functions, adding the following to the above files:
// *** template.cpp ***
// ...
// wrap them [optionally also inline the templates]
int square (int x) { return square<> (x); }
double square (double x) { return square<> (x); }
That compiles and works as expected. However, this seems like a hack to me. There should be something more elegant than this available in C++ and template syntax.
Any help or hints would be much appreciated.