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

Viewed 466
class Myclass
{

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

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

    }

};

I wrote above class and tried to compile it. I got following error:

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

Then I moved out my static function out side the class like this:

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>();
  }

};

I got following error:

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

Then of course I inline my specialized function and it worked.

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>();
  }

};

My questions are:
1) Why can't we specialize static member functions inside the class.
2) Why can't we have static template specialized functions

2 Answers
Related