两个晦涩的MSVC ++ 2017(15.9.28307.905)编译器问题

时间:2020-01-21 14:22:49

标签: c++ templates compiler-errors template-specialization

第一个问题:

#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更成问题?

0 个答案:

没有答案
相关问题