Here is some simplified code to demonstrate the problem I have.
I have a template function for which I only want to compile certain fixed instances.
Function declarations:
// *** template.h *** int square (int x); double square (double x);
Definitions:
// *** template.cpp ***
And an example of use:
// *** main.cpp ***
Attempting to compile this results in link errors, approximately:
main.obj: unresolved external symbol "int square (int)" referenced by main
I understand what the problem is: the function signatures of my explicit template instances do not match those contained in the header file.
What is the syntax for declaring explicit template instances, please? I do not want to forward the template definition declaration or move the template definition to the header file.
For what it's worth, I have a workaround that should use wrapper functions by 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); }
This compiles and works as expected. However, this seems like a hack to me. There must be something more elegant than what is available in the C ++ syntax and the template.
Any help or tips would be greatly appreciated.
source share