This is a followup to http://stackoverflow.com/questions/2050900/c-templates-prevent-instantiation-of-base-template
I use templates to achieve function overloading without the mess of implicit type conversions: declare the function template, define desired specializations (overloads). all is well except wrong code does not produce errors until the link phase:
lib.hpp:
template<class T> T f(T v);
lib.cpp:
#include "lib.hpp"
template<> long f(long v) { return -v; }
template<> bool f(bool v) { return !v; }
main.cpp:
#include <iostream>
#include "lib.hpp"
int main()
{
std::cout
<< f(123L) << ", "
<< f(true) << ", "
<< f(234) << "\n"
;
}
gcc output:
c++ -O2 -pipe -c main.cpp
c++ -O2 -pipe -c lib.cpp
c++ main.o lib.o -o main
main.o(.text+0x94): In function `main':
: undefined reference to `int get<int>(int)'
I'd like to have it fail during compilation of main.cpp. Can I somehow declare only specializations actually implemented?
What are my options? The target is C++03, and I'm mainly interested in gcc-4.x and VC9.