40

I'm new to templates so maybe this is a trivial thing but I cannot get it to work. I'm trying to get partial specialization of a class member function. The shortest code would be:

template <typename T, int nValue> class Object{
private:
    T m_t;
    Object();
public:
    Object(T t): m_t(t) {}
    T Get() { return m_t; } 
    Object& Deform(){ 
        m_t*=nValue; 
        return *this;
    }
};

template <typename T>
Object<T,0>& Object<T,0>::Deform(){
    this->m_t = -1;
    return *this;
}

int main(){
    Object<int,7> nObj(1);
    nObj.Deform();
    std::cout<<nObj.Get();
}

I tried with nonmember functions and that's worked fine. What also works fine is full specialization of a member function.

But, whenever I try with partial spec. of a member function I get error of the form:

PartialSpecification_MemberFu.cpp(17): error: template argument
list must match the parameter list Object<T,0>& Object<T,0>::Deform().

Would appreciate any help :-)

Simon Righley
  • 4,538
  • 6
  • 26
  • 33

3 Answers3

46

You cannot partially specialize only a single member function, you must partially specialize the whole class. Hence you'll need something like:

template <typename T>
class Object<T, 0>
{
private:
    T m_t;
    Object();
public:
    Object(T t): m_t(t) {}
    T Get() { return m_t; } 
    Object& Deform()
    {
        std::cout << "Spec\n";
        m_t = -1;
        return *this;
    }
};
Yuushi
  • 25,132
  • 7
  • 63
  • 81
21

14.5.5.3.1. The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization. The template argument list of a member of a class template partial specialization shall match the template argument list of the class template partial specialization.

In other words: no partially specialized member without partially specialized class.

Red XIII
  • 5,771
  • 4
  • 28
  • 29
15

Unfortunately, you can't partially specialize member function of a template class. You may either partially specialize the whole class or use inheritance. You may also use both:

template <typename T, int nValue>
class Object {
protected:
    T m_t;
public:
    Object() = delete;
    Object(T t): m_t(t) {}
    T Get() { return m_t; }
    Object& Deform() {
        m_t *= nValue; 
        return *this;
    }
};

template <typename T>
class Object<T,0> : public Object<T,1> {
public:
    using Object<T,1>::Object;

    Object& Deform() {
        this->m_t = -1;
        return *this;
    }
};
Maksym Ganenko
  • 1,288
  • 15
  • 11
  • You can also _[fully specialize](https://en.cppreference.com/w/cpp/language/template_specialization)_ the function body, if the template parameters lend themselves to that. Suppose T was a character type; it might be better to specialize `Object::Deform()` on ``, `` and ``. – Spencer Mar 30 '23 at 13:05