仅在头文件中静态模板化成员函数的模板行为

template behavior for static templatized member function in the header file only

class Myclass
{

    template <typename T>
    static T func()
    {
        T obj;
        return obj;
    }

    template<>
    static int func<int>()
    {

    }

};

我在上面写了class并尝试编译它。 我收到以下错误:

error: explicit specialization in non-namespace scope 'class Myclass'
error: template-id 'func' in declaration of primary template

然后我将我的静态函数从 class 移出,如下所示:

namespace helper 
{
    template <typename T>
    static T func()
    {
        T obj;
        return obj;
    }

    template<>
    static int func<int>()
    {

    }
}

class Myclass
{
  template <typename T>
  static T func()
  {
      helper::func<T>();
  }

};

我收到以下错误:

error: explicit template specialization cannot have a storage class
In static member function 'static T Myclass::func()':

当然,我内联了我的专用函数并且它起作用了。

namespace helper 
{
    template <typename T>
    static T func()
    {
        T obj;
        return obj;
    }

    template<>
     inline int func<int>()
    {

        return 1;
    }
}

class Myclass
{
  template <typename T>
  static T func()
  {
      helper::func<T>();
  }

};

我的问题是:
1) 为什么我们不能特化 class.
中的静态成员函数 2) 为什么我们不能有静态模板专用函数

据我所知,C++ 不允许 class 级别的成员模板特化。特化必须在名称空间级别提供,因此您可以在 class:

之外声明特化
// myclass.h
class MyClass
{
public:

    template <class T>
    void test(T )
    {
        std::cout << "non-specialized test" << std::endl;
    }

    template <class T>
    static T myfunc()
    {
        std::cout << "non-specialized myfunc" << std::endl;
        return T{};
    }
};


template <>
inline void MyClass::test(double t)
{
    std::cout << "specialized test" << std::endl;
}

template <>
static double MyClass::myfunc();

/*template <>
static inline void MyClass::myfunc(double )
{
    std::cout << "specialized myfunc" << std::endl;
}*/

然后你在源文件中提供实现:

// myclass.cpp

template <>
static double MyClass::myfunc()
{
    std::cout << "specialized myfunc" << std::endl;
    return 0.0;
}

或者,您可以在头文件中内联 myfunc()(就像 test() 函数一样)。

关于你的第二个问题,我在VS2015上试过了,成功了。唯一的问题是您缺少 return 个值。

老实说,这两个问题的真正答案可能是 "because."

你可以特化一个成员函数模板,它只需要在 class 之外并且不能使用 static 关键字:

struct Myclass {
    template <class T>
    static T func() {
        T obj{};
        return obj;
    }
};

template <>
int Myclass::func<int>() { return 42; }

两者都是语法原因。这只是您必须记住的事情之一。