第一个问题:
#include <iostream>
class Tester
{
public:
template< typename T >
static T f1();
template< typename T >
static void f2( const T& p = Tester::f1< T >() );
};
template<>
float Tester::f1()
{
return 0.0f;
}
template< typename T >
void Tester::f2( const T& p )
{
std::cout << p;
}
int main( int argc, char* argv[] )
{
Tester::f2< float >();
return EXIT_SUCCESS;
}
使用上述编译器编译输出:
main.obj:-1: error: LNK2019: unresolved external symbol "public: static class $RBAAB __cdecl Tester::f1<`template-type-parameter-1'>(void)" (??$f1@$RBAAB@@Tester@@SA?AV$RBAAB@@XZ) referenced in function main
debug\test.exe:-1: error: LNK1120: 1 unresolved externals
MSVC ++ 2015(14.0)报告编译此代码没有错误。仅当f1
是类的一部分时,才产生错误。 f2
可以在任何地方-一个独立的函数或一个类成员。
如果您定义f1
而不是专门化它,那就是替换返回类型float
:
template<>
float Tester::f1()
{
return 0.0f;
}
与T
:
template< typename T >
T Tester::f1()
{
return 0.0f;
}
您将收到以下错误:
error: C1001: An internal error has occurred in the compiler.
再次使用MSVC ++ 2015不会产生任何错误。
任何想法,为什么MSVC ++ 2017更成问题?