I have been lately been learning template meta programming in C++. After checking calculating factorials example, was wondering if the same thing could be done only with template functions rather than template classes. My first attempt is shown below
#include <stdio.h>
#include <iostream>
using namespace std;
template <int t>
int multiply(t)
{
return (multiply(t-1) * t);
}
template <>
int multiply(1)
{
return 1;
}
int main () {
cout << multiply(5) << endl;
return 0;
}
But I get the following compiler errors
temp.cpp:7: error: template declaration of 'int multiply'
temp.cpp:14: error: expected ';' before '{' token
temp.cpp: In function 'int main()':
temp.cpp:19: error: 'multiply' was not declared in this scope
Can I do such template metaprogramming using template functions? Is this allowed ?
int multiply(t)doesnt make sense, whether template or not