模板参数列表问题太少

Too few template-parameter-lists problem

本文关键字:问题 列表 参数      更新时间:2023-10-16

谁能告诉我如何使以下伪代码与GCC4兼容?我想知道它在 MSVC 下是如何工作的......

typedef int TypeA;
typedef float TypeB;
class MyClass
{
// No base template function, only partially specialized functions...
    inline TypeA myFunction<TypeA>(int a, int b) {} //error: Too few template-parameter-lists
    template<> inline TypeB myFunction<TypeB>(int a, int b) {}
};

编码该结构的正确方法是:

typedef int TypeA;
typedef float TypeB;
class MyClass
{
    template <typename T> 
    T myFunction( int a, int b );
};
template <> 
inline TypeA MyClass::myFunction<TypeA>(int a, int b) {}
template <> 
inline TypeB MyClass::myFunction<TypeB>(int a, int b) {}

请注意,模板成员函数必须在类声明声明,但专用化必须在类声明之外的命名空间级别定义。