So I’ve been trying to figure out how to use template specialization but ran into an unexpected compiler error. The more I check the syntax the more it looks correct so clearly I’m missing something. All I’m trying to do is create two function templates that are specialized and compare them to overloaded functions. The error I get is as follows: 'OverloadedFunk' could not be resolved on line 28 and 35. Here is the code I trying to rock:
#include <iostream>
using namespace std;
enum ErrorCode {
ERROR_NONE = 0, ///< No errors
SOME_FAILURE_01,
SOME_FAILURE_02,
INVALID_STATUS,
ERROR_UNKNOWN,
};
template<typename _to, typename _from>
inline int OverLoadedFunk(_from const &arg, _to &dest)
{
cout << "OverLoadedFunk3 - Template to from";
return 0;
}
template<>
inline int OverloadedFunk(const int &from, std::string &dest) //Line 28
{
cout << "OverloadedFunk1 - int to string";
return 0;
}
template<>
inline int OverloadedFunk(const ErrorCode &from, std::string &dest) //Line 35
{
cout << "OverloadedFunk2 - enumeration to string";
return 0;
}
int main() {
std::string localDest = "test";
int localFrom = 1234;
OverloadedFunk(localFrom, localDest);
return 0;
}
What exactly am I doing wrong here? I know I could use overloaded functions instead, but I’m trying to test difference between specialization and overloaded functions so that won’t help me in this case. All help is greatly appreciated.
Your primary function template is named
OverLoadedFunk, but your specializations are namedOverloadedFunk– C++ is case-sensitive!