I have a function with an integer template parameter:
template<int N>
int func() {
return N;
}
I explicitly instantiate the template for all template arguments I might provide:
template int func<1>();
template int func<2>();
template int func<3>();
//...
I then want to call func with a run-time specified integer n:
//This code does not compile
int main() {
int n;
std::cin >> n;
// check that n is one of the allowed values
std::cout << func<n>(); // can fail if given a bad n
return 0;
}
Are there modifications I can make to this code to call func<n>() with n specified at run-time (say 0 < n < 20)?