Question:
What is equivalent to static polymorphism?
Declare a function template without implementation. Create implementations only for the types you want to support.
template<typename T> string f();
template<> string f<float>() { return "float"; }
f<int>();
f<float>();
Update
Usage static_assert:
#include <string>
using std::string;
template<typename T> string f() { static_assert((sizeof(T) == 0), "Not implemented"); return "";}
template<> string f<float>() { return "float"; }
int main()
{
f<int>();
f<float>();
return 0;
}
Compiler Report:
g++ -std=c++11 -Wall socc.cc -o socc
socc.cc: In function ‘std::string f()’:
socc.cc:6:35: error: static assertion failed: Not implemented
<builtin>: recipe for target `socc' failed
source
share