When I specialize an int-templated function with a negative value defined in hexadecimal, Visual Studio 2010 give me a strange "1 unresolve externals" link error.
Still it works fine if:
- I define the negative int value in decimal form.
- I define the template argument with "unsigned int" instead of "int".
- I use directly "DefaultMsg" templated-function. I.e. it only fails when I use "NewMsg" templated-function which calls "DefaultMsg" templated-function.
Then I have some workaround here, but I still do not understand what is going wrong. Am I doing something bad ? Or is it a tricky compiler bug/limitation case ?
Here is a sample code:
// Compile condition: define a code in hexa or decimal
#define DEFINEHEXANOTWORKING 1
// Code
#define CODE1 1
#define CODE2 0x2
#if !DEFINEHEXANOTWORKING
// Here define a negative code in decimal mode
#define CODE3 -1
#else
// Here define a negative code in hexadecimal mode
#define CODE3 0xFFFFFFFF
#endif
// Default message declaration
template<int r> __forceinline const char* DefaultMsg();
// Default message specialization
template<> __forceinline const char* DefaultMsg<CODE1>() { return "Message 1."; }
template<> __forceinline const char* DefaultMsg<CODE2>() { return "Message 0x2."; }
template<> __forceinline const char* DefaultMsg<CODE3>() { return "Message -1/0xFFFFFFFF."; }
// New template message
template<int r> __forceinline const char* NewMsg() { return DefaultMsg<r>(); }
// Main test
int _tmain(int argc, _TCHAR* argv[])
{
// Print default messages, all working in all cases
printf("Default messages:\n");
printf("CODE1: %s\n", DefaultMsg<CODE1>());
printf("CODE2: %s\n", DefaultMsg<CODE2>());
printf("CODE3: %s\n", DefaultMsg<CODE3>());
// Print new messages, not working if defined in hexa mode
printf("\nNew messages:\n");
printf("CODE1: %s\n", NewMsg<CODE1>());
printf("CODE2: %s\n", NewMsg<CODE2>());
printf("CODE3: %s\n", NewMsg<CODE3>()); // Not compiling line in hexa mode
_getch();
return 0;
}
The compiler error:
error LNK2019: unresolved external symbol "char const * __cdecl DefaultMsg<-1>(void)" (??$DefaultMsg@$0?0@@YAPBDXZ) referenced in function "char const * __cdecl NewMsg<4294967295>(void)" (??$NewMsg@$0PPPPPPPP@@@YAPBDXZ)
1>D:\§CELII\Tests de programmation\2017-08-16 - Test template\Debug\2017-08-16 - Test template.exe : fatal error LNK1120: 1 unresolved externals