Template behavior for a static templatized member function only in the header file

class Myclass
{

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

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

    }

};

      

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

error: explicit specialization in non-namespaced "class Myclass"
error: template-id 'func' in primary template declaration

Then I pulled my static function out of 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 the following error:

error: explicit template specification cannot have storage class
 In the static T Myclass :: func () member function:

Then of course I turned on my custom 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 a class. 2) Why we don't have specialized static template functions

+3


source to share


2 answers


To be honest, the real answers to both questions are probably "because".

You can specialize the member function template, it just needs to be outside the class and cannot use the keyword static

:



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

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

      

Both are mostly grammatical reasons. This is just one of those things that you should remember.

+1


source


As far as I know, C ++ does not allow specialization of the member template at the class level. Specializations must be exposed at the namespace level, so you can declare a specialization outside the 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;
}*/

      

Then you provide an implementation in your source file:



// myclass.cpp

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

      

Alternatively you can include myfunc()

in the header file (as a function test()

).

As for your second question, I tried it in VS2015 and it worked. The only problem is that you are missing the return values.

0


source







All Articles