《C++ Templates The Complete Guide》中的IsFunctionT编译错误

    技术2022-05-20  58

    《C++ Templates The Complete Guide》书中,第19章类型区分有一个例子编译不过,代码如下:

    template<typename T> class IsFunctionT { private: typedef char One; typedef struct { char a[2]; } Two; template<typename U> static One test(...); template<typename U> static Two test(U (*)[1]); public: enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 }; enum { No = !Yes }; }; 

    错误原因为Error 1 error C2784: 'IsFunctionT<T>::Two IsFunctionT<T>::test(U (*)[1])' : could not deduce template argument for 'U (*)[1]' from 'int'。

    把IsFunctionT<T>删除,就没有问题了,不知道为什么?

    修改后的代码如下:

    template<typename T> class IsFunctionT { private: typedef char One; typedef struct { char a[2]; } Two; template<typename U> static One test(...); template<typename U> static Two test(U (*)[1]); public: enum { Yes = sizeof(test<T>(0)) == 1 }; enum { No = !Yes }; }; 


    最新回复(0)