当我专门化一个带有以十六进制定义的负值的int-templated函数时,Visual Studio 2010给了我一个奇怪的“1 unresolve externals”链接错误。
如果符合以下情况仍然可以正常工作:
然后我在这里有一些解决方法,但我仍然不明白出了什么问题。我做坏事吗?或者它是一个棘手的编译器错误/限制案例?
以下是示例代码:
// 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;
}
编译错误:
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